0

I was doing boundary check of a 2D array which is array[8][LIMIT]; Then I tried several times and found the LIMIT variable should not be larger than 89489404, otherwise there will be an exception like this:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at testLimit.testLimit.main(testLimit.java:9)

I know that 2D array should be allocated the memory in continuous space but just out of curiosity, how do Java decide this number? If I need to new an 2D array based on user input, how do I check the boundary so that no OutOfMemoryError will be thrown?

Zip
  • 809
  • 2
  • 14
  • 30

1 Answers1

1

Maximum size of the array depends on how much Memory got allocated to JVM via -Xmx startup parameter. If you allocate more memory to the VM it can get more space allocated before running out of available memory. But absolute maximum is 2^31-1 since java uses signed integers and indicies have to be positive

masterX244
  • 221
  • 2
  • 11
  • Thanks for answering. So if I don't change the setting to VM, the max size should stay the same, can I assume that the max size should be 8* 89489404 for my case? And `array[x][y]` where x*y=8* 89489404 should be the limit? – Zip Feb 14 '15 at 21:28
  • should be the same +- a few elements depending on which Java version/implementation is used. – masterX244 Feb 14 '15 at 21:42