How should I deal with a very large array in Java?

前端 未结 7 1782
情深已故
情深已故 2021-02-04 13:48

I have an algorithm which currently allocates a very large array of doubles, which it updates and searches frequently. The size of the array is N^2/2, where N is the number of

7条回答
  •  野性不改
    2021-02-04 14:27

    Be aware that some operating systems have better support for memory mapping than others.

    I would be tempted to do this:

    1. Put all your array gets/puts behind an object interface (if they aren't already) thus freeing you up to easily change the implementation.
    2. Use an array of SoftReferences where each SoftReference points to the array of doubles for that row. Use a ReferenceQueue to save the arrays to disk when the GC kicks them out. When get() returns null, retrieve from disk.

    You might find you have more control over performance that way - the -Xmx can be tweaked as desired.

提交回复
热议问题