I’m making a Java application that uses the Slick library to load images. However, on some computers, I get this error when trying to run the program:
Exception in thread "main" java.lang.OutOfMemoryError
at sun.misc.Unsafe.allocateMemory(Native Method)
at java.nio.DirectByteBuffer.<init>(DirectByteBuffer.java:99)
at java.nio.ByteBuffer.allocateDirect(ByteBuffer.java:288)
at org.lwjgl.BufferUtils.createByteBuffer(BufferUtils.java:60)
at org.newdawn.slick.opengl.PNGImageData.loadImage(PNGImageData.java:692)
at org.newdawn.slick.opengl.CompositeImageData.loadImage(CompositeImageData.java:62)
at org.newdawn.slick.opengl.CompositeImageData.loadImage(CompositeImageData.java:43)
My VM options are:
-Djava.library.path=lib -Xms1024M -Xmx1024M -XX:PermSize=256M -XX:MaxPermSize=256M
The program loads a few large images (1024 x 768 resolution) at the beginning.
Any help to solve this problem would be greatly appreciated.
The
OutOfMemoryErrorsimply indicates that JVM has run out of memory. The first line of the stacktrace isn’t really relevant here as it’s just “by coincidence” exactly there where JVM starts to run out of memory, with all Garbage Collecting in vain.There are basically two solutions to this:
byte[]for too long and so on).Point 1 is easy to do, but not always the solution if there’s apparently a memory leak in the code. Point 2 is best to be nailed down with help of a Java profiler.