I have a question regarding the Java heap space.
I have a program that uses geotools to create a graph from a street network.
I do some stuff with that but I run into a OutOfMemoryError but I am unsure why. I use the following code to get the amount of free memory.
Runtime rt = Runtime.getRuntime();
long maxMb = rt.freeMemory()/(1024*1024);
System.out.println("Your JVM has " + maxMb + " MB of memory left");
And when I run my program, there seems to be enough memory (see output). This console output is done right before the OutOfMemory Error occurs. So what is going wrong? When I enlarge the Java heap space, the OutOfMemory Error occurs nevertheless (but with 1461 MB left). I have no idea why that happens? Any ideas?
Your JVM has 981 MB of memory left
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.grow(Unknown Source)
at java.util.ArrayList.ensureCapacityInternal(Unknown Source)
at java.util.ArrayList.add(Unknown Source)
at masterthesis.datastructures.Stroke.addStrokeSegment(Stroke.java:78)
at masterthesis.algorithms.StrokeBuilder.buildStroke(StrokeBuilder.java:145)
at org.geotools.test.Main.main(Main.java:109)
Since the output indicates that you have a lot of memory left and the error ocurrs during an array list growth my guess is that you have a really huge arraylist that you put objects in. When the JVM tries to grow the list you do not have enough heap space to accomodate the doubling in size of the list.
I would investigate how many objects you actually put in your list with
addStrokeSegment().