Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine what objects are being collected by the garbage collector?

I have significant garbage collection pauses. I'd like to pinpoint the objects most responsible for this collection before I try to fix the problem. I've looked at the heap snapshot on Chrome, but (correct me if I am wrong) I cannot seem to find any indicator of what is being collected, only what is taking up the most memory. Is there a way to answer this empirically, or am I limited to educated guesses?

like image 795
shino Avatar asked Sep 22 '12 21:09

shino


People also ask

How does the garbage collector know if an object can be collected?

The garbage collector uses a traversal of the object graph to find the objects that are reachable. Objects that are not reached in this traversal are deemed garbage, even if they are part of a cycle of references.

Which method is used to garbage collect an object?

gc() method: Runtime class allows the application to interface with the JVM in which the application is running. Hence by using its gc() method, we can request JVM to run Garbage Collector.

How does the garbage collector works?

When an object is no longer used, the garbage collector reclaims the underlying memory and reuses it for future object allocation. This means there is no explicit deletion and no memory is given back to the operating system.


1 Answers

In chrome profiles takes two heap snapshots, one before doing action you want to check and one after.

Now click on second snapshot.

On the bottom bar you will see select box with option "summary". Change it to "comparision".

Then in select box next to it select snaphot you want to compare against (it should automaticaly select snapshot1).

As the results you will get table with data you need ie. "New" and "Deleted" objects.

like image 176
Rafał Łużyński Avatar answered Oct 17 '22 08:10

Rafał Łużyński