Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine where an object was instantiated in VisualVM Heap Dump

I have a bug in my program that is generating a lot of String instances (7000+ in Heap according to VisualVM). I am trying to isolate which class is responsible for this so I can understand why it is happening.

The following is an example of the heap dump for String:

enter image description here

How do I figure out which class is responsible for generating each of the String? I am working with about 40 classes together so I would like to be able to identify the culprit class through VisualVM if possible.

like image 547
user2763361 Avatar asked Jan 10 '14 11:01

user2763361


People also ask

Where does VisualVM store heap dumps?

Heap dumps are displayed in the heap dump sub-tab in the main window. You can open binary format heap dump files (. hprof) saved on your local system or use Java VisualVM to take heap dumps of running applications.

How does VisualVM analyze heap dump?

One can right-click on any class in the "Classes" tab and select "Show in Instances View" to see details on each individual instance of the selected class. This is shown in the next screen snapshot. VisualVM provides several advantages when creating and analyzing heap dumps.

How do I monitor VisualVM?

Under the Local node in the Applications window, right-click the application node and choose Open to open the application tab. Click the Profiler tab in the application tab. Click Memory or CPU in the Profiler tab. When you choose a profiling task, VisualVM displays the profiling data in the Profiler tab.

How do you analyze a heap dump in Java?

We will first start the Memory Analyzer Tool and open the heap dump file. In Eclipse MAT, two types of object sizes are reported: Shallow heap size: The shallow heap of an object is its size in the memory. Retained heap size: Retained heap is the amount of memory that will be freed when an object is garbage collected.

How to generate a heap dump in visual VM?

One method for generating a heap dump in Visual VM is to simply right click on the desired process and select "Heap Dump". This method is shown in the next screen snapshot. This generates the heap dump as indicated by its name underneath the Java process.

What is JVM heap memory dump?

How to Generate JVM Heap Memory Dump? Java Heap dump is a snapshot of all java objects that are present in the JVM (Java Virtual Machine) at a certain point in time. The JVM allocates memory for objects which are class instances or arrays in the heap memory.

How do I open a Java dump file in VisualVM?

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. When you open a saved heap dump, the heap dump opens as a tab in the main window.

How do I take a heap dump from a running program?

Taking a Heap Dump. You can use Java VisualVM to take a heap dump of a local running application. When you use Java VisualVM to take a heap dump, the file is only temporary until you explicitly save it. If you do not save the file, the file will be deleted when the application terminates.


1 Answers

You should have a look at this Q/A: How to view memory allocation stacktrace in Java VisualVM - a heap profile, which is a "memory allocation stacktrace" is what you want.

Here is a nice tutorial : Analyzing Memory Leak in Java Applications using VisualVM.

In general, there is several ways howto diagnose this problem, using a Java profiler:

  • use CPU profiling, the best in a instrumentation mode, this will record all method invocations, look at String.<init> - this will show you all the possible methods that execute new String(). The best tools are e.g. jProfiler, Java Mission Control, where you can easily identify all the callers of the String.<init>. The same applies for VisualVM. I think using CPU profiling is the most straightforward way of idenifying where in the code the new String occurs the most.

  • collect a heap dump in MAT, and analyze that, this will allow you to traverse the heap, you will again search for String finding all the references to a String object.

  • use Heap profiling option of a Java profiler (e.g. VisualVM), is similar to collecting a heap dump, only this heap profiling is usually collected online. So just enable heap profiling in VisualVM and follow the references from the String instances (i.e. find objects referencing String).

like image 156
Aleš Avatar answered Oct 21 '22 16:10

Aleš