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

16,694

Solution 1

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/

Solution 2

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.

Solution 3

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.

Solution 4

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.

Solution 5

Use a profiler like JProfiler or YourKitProfiler

Share:
16,694
daxsorbito
Author by

daxsorbito

Symbian/C++/Java/Python/Perl developer, architect and general firefigher in the UK.

Updated on July 09, 2022

Comments

  • daxsorbito
    daxsorbito almost 2 years

    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.