Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

visualVM memory leak

Tags:

java

While using visualVM to detect memory leak, it allows us an Instance view as described below:

The Instance view displays object instances for a selected class. When you select an instance from the Instance pane, VisualVM displays the fields of that class and references to that class in the respective panes. In the References pane, you can right-click an item and choose Show Nearest GC Root to display the nearest garbage collection root object.

Can anybody tell me what the nearest garbage collection root object is and how to make use of this information to help identify the leaking spots. Thanks!

like image 998
flyingfromchina Avatar asked Oct 15 '22 12:10

flyingfromchina


2 Answers

You might find this Sun book/chapter on Garbage Collection useful, in particular this section which lists GC root as:

  • Temporary variables on the stack (of any thread)
  • Static variables (from any class)
  • Special references from JNI native code

In other words, GC roots are variables that can keep another object from being GCed by virtue of the root holding a reference to it.

like image 150
matt b Avatar answered Nov 10 '22 20:11

matt b


A GC root is a reference held in a static or local variable. A reference held in a root prevents the referenced object from being garbage collected.

In VisualVM you can use the "show nearest GC root" feature to help you track down references to the object that you believe should be garbage collected.

like image 43
Chris Lacasse Avatar answered Nov 10 '22 20:11

Chris Lacasse