Last night I was working on a little fun Clojure project that involved reading very large files. While reading and processing a 700MB file, my application crashed complaining that it had run out of memory. I had given it 500MB of RAM. What else does it need? Have you ever been in this situation where you are having Java memory issues and need to know exactly what’s going on? When this happens, your best friend is JMX and
jconsole.jconsole comes with the JDK so you already have it. To use these two together, you first need to start Clojure with the Java option -Dcom.sun.management.jmxremote. Once the app is running you can run jconsole which can be found in the bin directory of the JDK. This will open a window which will allow you to select the Java process to monitor. Once you select a process, you will see something like this:
This allows you to see the internals of the JVM as it is running. Armed with this information you should be able to figure out what is going on. After that, I'm sure you will go back to being mad at Java.
This is easy but I needed to make it a little easier. I updated my
clj script to add the option above to Java when I pass it the -Xjmx option.You can do a lot more with JMX and Clojure. If you wish to get into it in more detail, check out the jmx library in clojure.contrib by Stuart Halloway.

Hi,
ReplyDeleteYou may also want to take a look at jvisualvm - it's also in the bin/ directory of Sun JDK 6 (or if you haven't sun JDK you can download the stand-alone open source version of the tool from http://visualvm.dev.openjdk.net ).
With jvisualvm you will be able to take a heap dump, and analyse it graphically - finding out which objects have which references on which, finding the nearest GC root etc...
I often use jconsole to quickly diagnose a problem and jvisualvm to take the analysis further!
Cheers,
-- daniel
Daniel,
ReplyDeleteThanks for pointing this out. I have heard of it but not used it. I will give it a try the next time I run into a problem -- or maybe before then, just for fun.
Brenton