0

I wrote the following code to turn a one dimensional array to a 2D array. Whenever I use it for small size arrays it works fine, whenever the array gets too big the code throws a java.lang.OutOfMemoryError: Java heap space exception.

byte[][] twoD = new byte[(int)Math.ceil(xlArray.length / (double)4096)][4096];

Is it just a problem caused in the IDE as it doesn't have access to enough heap space on the machine or more than that?

trincot
  • 317,000
  • 35
  • 244
  • 286
Rakim
  • 1,087
  • 9
  • 21
  • 40
  • what is max value that xlArray length can have? which IDE ? java 7 or java 8? – SomeDude Feb 17 '16 at 21:15
  • This has to do with the JVM. Because all memory is managed for you in Java, it ensures that you can only use so much memory. When running the program you can specify how large you want your heap to be, but the limits are actually platform dependent (like [this person](https://developer.ibm.com/answers/questions/175172/why-can-i-not-set-a-maximum-heap-setting-xmx-over.html) asked). – callyalater Feb 17 '16 at 21:18
  • I see no reason to think that the problem is anything other than the exception indicates: there is not enough free space in your heap to allocate all the requested objects. If you're running this in an IDE then the appropriate solution depends on the IDE details. You may be able to specify that the program should run with a larger heap; you may need to increase the IDE's own heap; or perhaps you need a better IDE. – John Bollinger Feb 17 '16 at 21:18
  • [This post](http://stackoverflow.com/questions/3030263/increasing-the-jvm-maximum-heap-size-for-memory-intensive-applications) addresses this question as well. The default heap size in oracle is 64M (see [official documentation](http://www.oracle.com/technetwork/java/javase/gc-tuning-6-140523.html)). – callyalater Feb 17 '16 at 21:20
  • I am using java8 on IDEA IntelliJ. Trying to find the settings for the heap and update you – Rakim Feb 17 '16 at 21:39
  • following this link I tried to change the max heap size but cannot tell if it worked. It didn't make any difference for the execution for sure. I set it to 2g https://www.jetbrains.com/idea/help/increasing-memory-heap.html#d1366197e113 – Rakim Feb 17 '16 at 22:29

2 Answers2

1

An IDE (any IDE) usually spawns a child process which has its own memory settings. You can usually configure those in some kind of profile.

In Eclipse for example select the menu "Run" -> "Run Configurations". Inside a "Java Application" configuration go to the "Arguments" tab.

Inside you can set VM arguments. Take a look at the Java Non-Standard Options for information about memory management.

Brian
  • 872
  • 8
  • 16
0

When calling java use:

  • -Xms specifies the initial Java heap size.
  • -Xmx the maximum Java heap size.(The default size is 128mb).

Take a look here: https://docs.oracle.com/javase/8/docs/technotes/tools/windows/java.html

If you are using an ide like netbeans, eclipse etc you can find Run Configurations options -> Arguments -> VM Arguments and set Xmx and (or) Xms.


How can I increase the JVM memory?

Community
  • 1
  • 1
a.s.p.
  • 328
  • 1
  • 3
  • 12
  • Where did you find the 128mb default size for the heap? I looked in the [Oracle documentation](http://www.oracle.com/technetwork/java/javase/gc-tuning-6-140523.html) and found 64m. Has it changed? – callyalater Feb 17 '16 at 21:39
  • Maybe it had been changed. But it was 128mb. – a.s.p. Feb 18 '16 at 16:09