Managing Memory in Java

September 4th, 2008

Doodle of a beetle with a fiddle.

Getting memory management to play along in Java is a whole other chapter than it was in C and C++. And by that I mean simpler and better. Though a few people would still like to allocate and deallocate memory themselves, most agree that garbage collectors are a step forward (afaik). Whatever your stance on the case, GC is in Java, that’s the way it is. There are tweaks, and even replacement collectors, to get the garbage collecting to be done in tune with your code.

I’ll have a look on simpler aspects of managing memory in Java. For one - adding more available (heap) memory for your application. You’ll usually have need of this if you are writing big, memory hungry programs. Anything gathering and working on large amounts of data. Your hint will come from a thrown exception blaming java.lang.OutOfMemoryError: Java heap space.

Assigning more memory to Java is as easy as supplying one or two parameters to java when starting your program. There is -Xmx= for maximum heap size in bytes, and -Xms= for initial heap size. Since increasing the heap take some resources, it is suggested setting the initial size at something that should be enough for use, and maximum to something that should be enough - period.

These values are specified in bytes, but you could do it in kB or MB by adding a suffix, like this: java -Xms=64m -Xmx=256m MyProgram.

A crash citing the JVM out of memory could also easily be a (dire) warning about you having a memory leak or two in your code. Java isn’t invulnerable to those, though they are harder to get by accident. One usual suspect are Listeners, setting up a listener and then forgetting about it when the work is done could spell an early end to the session. Misbehaving (self-coded) collections are also a common way of stopping the garbage collectors from doing their work.

Detecting memory leaks are not that easy in Java. Since your program runs in a JVM, task managers in your OS will not report on your program, but on the virtual machine it is running in. To have a look at how your memory consumption is, you are in need of a Java profiler of some kind. An easy alternative is to fire up your program with java -Dcom.sun.management.jmxremote MyProgram, and then running the tool Sun supplies with Java (from 1.5 up): jconsole. This program is located where you have the JDK installed - usually C:\Program Files\Java\jdk[version]\bin on Windows.

Happy hunting.