How can I see what is in my heap in Java?

0 votes
asked Sep 28, 2008 by free-wildebeest

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.

7 Answers

0 votes
answered Sep 8, 2008 by kohlerm

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.

0 votes
answered Sep 28, 2008 by free-wildebeest

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

0 votes
answered Sep 28, 2008 by skaffman

Use a profiler like JProfiler or YourKitProfiler

0 votes
answered Sep 28, 2008 by cagcowboy
0 votes
answered Sep 28, 2008 by tom

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/

0 votes
answered Sep 29, 2008 by kire-haglin

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.

0 votes
answered Sep 15, 2017 by mkasberg

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.

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...