How to perform JVM memory analysis

JVM memory and garbage collection settings are a black box for most developers, no matter what language they work in. Garbage collection in Java is a very specialized field; very few people know much about it at all. Ideally, each application should have JVM settings that are particularly tuned for that app to manage memory allocation and GC in the most efficient way possible. But most of us don’t have a clue how that is done.

The key to working out what those settings should be for a particular application is logging, even for an expert. Or should I say, especially for an expert. Just as an expert programmer would not attempt to debug an application without a detailed error message or stack trace, a JVM wizard won’t be able to magically guess what the JVM is doing under the hood without a detailed GC log file.

So, the main thing the rest of us JVM noobs need to know is how to generate that log file.

There are a group of settings you can add to your JVM parameters to enable logging that will look something like the following:

-XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=3 -XX:GCLogFileSize=25m -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCCause -XX:+PrintGCApplicationStoppedTime -XX:+PrintTenuringDistribution -verbose:gc -Xloggc:gc.log

The second thing the rest of us need to know is what to do with that log file once it is generated. Fortunately, there are tools developed by JVM experts that will read and analyze the GC logs for us. Those tools will come with a recommended set of JVM parameters to set for logging, and generally will also recommend how much data they will need, a day, a few days, etc.

Here’s my favorite tool, if you can imagine a drum roll … … ta da!

To use it, you download and install the tool, and upload your GC log into it, and you’ll get expert insight into how your JVM GC is behaving and what you should do to improve it.

It’s not open source or free, but … a JVM expert will cost a lot more, and he’ll probably be using Censum or similar anyway, and simply forwarding the recommendations from the tool to you with a little bit of flavoring on top from his or her experience. Maybe that experience is needed in certain cases, I don’t know, but if you are having memory issues and need to get to the bottom of them, I’d start with Censum and see where you get. There’s a mailing list for questions, and the folks there are very helpful. The best part is it’s plug and play. You can go from absolute idiot to relatively decent in terms of your JVM memory params with little effort.

If you want to go the open source route, then is available. The drawback here is that it provides less proactive information, so expect to spend a lot of time digging around for GC knowledge and experience.

There may certainly be other options for analyzing GC logs, but generating a GC log file and plugging it into a tool is the basic approach any developer should, ideally, be aware of.

1 Like