Follow Us
Contribute
  • Register
Errorbase is a collaborative writing project, that aims to build the largest trouble-shooting guide about all kinds of error messages.

Categories

+2 votes

This error message is shown when there is no enough space for allocating a new object in java heap. The below code will definitely throw this error:

ArrayList<String> arr = new ArrayList<String>();
while (true) {
arr.add("someStringHere");
}
requested by (170k points)

1 Solution

0 votes

In the thrown exception you can see the line number of the problem. For the above example, the thrown exception is like this:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Unknown Source)
at java.util.Arrays.copyOf(Unknown Source)
at java.util.ArrayList.ensureCapacity(Unknown Source)
at java.util.ArrayList.add(Unknown Source)
at SomePackage.SomeClass.main(SomeClass.java:14)

For this example the problem has occured in line 14. By this way you can understand when the overflow has occured and debugging becomes easier. If you are sure that there is no problem with the code, then you can change the java heap space via command line:
C:\> java -Xmx256m -jar SomeApp.jar
This command increases the java heap space to 256MB. By default java heap space is 128 MB.

solved by (1.6k points)
...