Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Huge arrays throws out of memory despite enough memory available

Using the -Xmx1G flag to provide a heap of one gigabyte, the following works as expected:

public class Biggy {
    public static void main(String[] args) {
        int[] array = new int[150 * 1000 * 1000];
    }
}

The array should represent around 600 MB.

However, the following throws OutOfMemoryError:

public class Biggy {
    public static void main(String[] args) {
        int[] array = new int[200 * 1000 * 1000];
    }
}

Despite the array should represent around 800 MB and therefore easily fit in memory.

Where's the missing memory gone?

like image 327
dagnelies Avatar asked Sep 04 '11 10:09

dagnelies


1 Answers

In Java you typically have multiple regions (and sub regions) in the heap. You have a young and tenured region with most collectors. Large arrays are added to the tenured area straight away however based on your maximum memory size, some space will be reserved for the young space. If you allocate memory slowly these regions will resize however a large block like this can simply fail as you have seen.

Given memory is usually relatively cheap (not always the case) I would just increase the maximum to the point where you would want the application fail if it ever used that much.

BTW: If you have a large structure like this you might consider using direct memory.

IntBuffer array = ByteBuffer.allocateDirect(200*1000*1000*4)
                            .order(ByteOrder.nativeOrder()).asIntBuffer();

int a = array.get(n);
array.put(n, a+1);

Its a bit tedious to write but has one big advantage, it uses almost no heap. (there is less than 1 KB over head)

like image 113
Peter Lawrey Avatar answered Nov 05 '22 20:11

Peter Lawrey