I'm dealing with huge arrays in my application and need to resize them.
Let's say you have an array of 2Gb and you want to resize it to 3Gb. Is there a way to resize it without needing temporarily 5Gb?
For instance, given a 1Gb heap using the -Xmx1G
flag:
public class Biggy {
public static void main(String[] args) {
int[] array;
array = new int[100 * 1000 * 1000]; // needs 400Mb, works
array = null; // needed for GC
array = new int[150 * 1000 * 1000]; // needs 600Mb, works
array = null; // needed for GC
array = new int[100 * 1000 * 1000]; // needs 400Mb, works
array = Arrays.copyOf(array, 150 * 1000 * 1000); // needs 1000Mb, throws out of memory
}
}
So, is there a way to resize arrays without requiring that extra temporary memory?