views:

909

answers:

7

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.

A: 

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

Free Wildebeest
+1  A: 

Use a profiler like JProfiler or YourKitProfiler

skaffman
+1  A: 

JProfiler worked very well for me....

http://www.ej-technologies.com/products/jprofiler/overview.html

cagcowboy
+4  A: 

If you need something free, try visual vm

André
+11  A: 

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/

Tom
Visual VM and the Netbeans Profiler allow you to do the same things as EMA.
James Schek
I don't know about netbeans, but I certainly haven't found 'shortest path to gc root' in visual vm.
Tom
+1 for EMA - lifesaver.
matt b
A: 

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.

Kire Haglin
+1  A: 

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.

kohlerm