Detecting Memory Leaks

Troubleshooting a problem over last weekend I ran into a classic memory leak. Yes, you can have memory leaks in Java! If you hold onto references to objects than the Java virtual machine cannot reclaim those objects. In my case, I was processing millions of records but not giving yielding and allowing an object cache to purge itself (my fault – IDE evidently didn’t copy over an updated config file because I didn’t make a code change). This problem led me to dig into Netbeans and profiling Java processes running as Windows’ services. While Netbeans is a great tool, it does a negative impact upon performance. Obviously a less intrusive tool is needed initially. Java 5 ships with jconsole which drills into a JVM and reports back on heap utilization, thread activity etc. JConsole connects to either local JVM or one on a remote machine. I have also used JConsole to track down a renegade thread that was caught in an IO loop as a result of security software checking for a buffer overrun.

To enable jconsole connectivity for an application simply adding the following VM parameters:

-Dcom.sun.management.jmxremote.port=8999
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false

Launch your application and then launch jconsole (jconsole should be in your path).
Below you can see screenshots from my ‘classic’ leak:

Classic Memory Leak

Classic Memory Leak

Published by rcuprak

Ryan Cuprak is an e-formulation analyst at Enginuity PLM and president of the Connecticut Java Users Group that he has run since 2003. At Enginuity PLM he is focused on developing data integrations to convert clients’ data and also user interface development. Prior to joining Enginuity he worked for a startup distributed-computing company, TurboWorx, and Eastman Kodak’s Molecular Imaging Systems group, now part of Carestream Health. At TurboWorx he was a Java developer and also a technical sales engineer supporting both presales and professional services. Cuprak has earned a BS in computer science and biology from Loyola University Chicago. He is a Sun Certified NetBeans IDE Specialist. He can be contacted at rcuprak@acm.org.