Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 286
Free Wildebeest Avatar asked Sep 28 '08 14:09

Free Wildebeest


People also ask

How do I check my Java heap?

Opening a Heap Dump File If you have a heap dump file saved on your local system, you can open the file in Java VisualVM by choosing File > Load from the main menu. Java VisualVM can open heap dumps saved in the . hprof file format.

Can we access heap memory in Java?

Heap space is used for the dynamic memory allocation of Java objects and JRE classes at runtime. New objects are always created in heap space, and the references to these objects are stored in stack memory. These objects have global access and we can access them from anywhere in the application.

How do I check my heap usage?

Checking Heap Usage:Sign on to the Weblogic Administration Console by entering the following URL in a browser: http://funoracle.lab:7001/console. Expand your WebLogic domain then expand Servers. Click the server you intend to monitor. Select the Monitoring tab, and the Performance sub-tab.

What is stored in Java heap?

Heap space is used for the dynamic memory allocation of Java objects and classes at runtime. New objects are always created in the heap space, and references to these objects are stored in the stack memory.


2 Answers

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/

like image 162
Tom Avatar answered Oct 22 '22 07:10

Tom


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.

like image 7
André Avatar answered Oct 22 '22 08:10

André