Question

I've managed to get a memory 'leak' in a java application I'm developing. When running my JUnit test suite I randomly get out of memory exceptions (java.lang.OutOfMemoryError).

What tools can I use to examine the heap of my java application to see what's using up all my heap so that I can work out what's keeping references to objects which should be able to be garbage collected.

Was it helpful?

Solution

VisualVM is included in the most recent releases of Java. You can use this to create a heap dump, and look at the objects in it.

Alternatively, you can also create a heapdump commandine using jmap (in your jdk/bin dir):

jmap -dump:format=b,file=heap.bin <pid>

You can even use this to get a quick histogram of all objects

jmap -histo <pid>

I can recommend Eclipse Memory Analyzer (http://eclipse.org/mat) for advanced analysis of heap dumps. It lets you find out exactly why a certain object or set of objects is alive. Here's a blog entry showing you what Memory Analyzer can do: http://dev.eclipse.org/blogs/memoryanalyzer/2008/05/27/automated-heap-dump-analysis-finding-memory-leaks-with-one-click/

OTHER TIPS

If you need something free, try VisualVM

From the project's description:

VisualVM is a visual tool integrating commandline JDK tools and lightweight profiling capabilities. Designed for both development and production time use.

This is a pretty old question. A lot of people might have started using IntelliJ since it was originally answered. IntelliJ has a plugin that can show memory usage called JVM Debugger Memory View.

Use a profiler like JProfiler or YourKitProfiler

Use the Eclipse Memory Analyzer

There's no other tool that I'm aware of any tool that comes close to it's functionality and performance and price (free and open source) when analysing heap dumps.

If you're using a system which supports GTK you could try using JMP.

You can try the Memory Leak Detector that is part of the JRockit Mission Control tools suite. It allows you to inspect the heap while the JVM is running. You don't need to take snapshots all the time. You can just connect online to the JVM and then see how the heap changes between garbage collections. You can also inspect objects, follow references graphically and get stack traces from where your application is currently allocating objects. Here is a brief introduction.

The tool is free to use for development and you can download it here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top