I initialize in main array.

int[] integers = new int[Integer.MAX_VALUE-2]; 

One element of type int is 4 bytes, such elements are 2147483645 and 4 bytes are for the size of the array. 4 * 2147483645 + 4 = 8.6 gigabytes. I allocate 10 gigabytes for VM -Xms10g -Xmx10g and get the exception OutOfMemoryError: Java heap space Why?

  • 2
    Do you have so much memory? And the x64 car? - Alexey Shimansky
  • The x64, 8 gigabyte machine is + I use the paging file, maybe the problem is due to the fact that I use paging? - diofloyk
  • if I allocate 13 gigabytes for a VM, everything works (the array is initialized), but I cannot understand why exactly 13 + gigabytes should be allocated to initialize such an array. If I correctly calculated the question should be required 8.6 gigabytes. - diofloyk
  • 2
    I suspect that it is connected with the organization of memory for the needs of GC (heap division into space for new and old objects). Try experimenting with the options -XX:NewRatio , etc. - zRrr
  • @zRrr, exactly, thanks, I will experiment - diofloyk

1 answer 1

Using VisualVM, make a heap dump to see how memory is used.