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

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


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.


With newer Chrome releases there is a new tool available that is handy for this kind of task:

The "Record Heap Allocations" profiling type. The regular "Heap SnapShot" comparison tool (as explained in Rafał Łużyński answers) cannot give you that kind of information because each time you take a heap snapshot, a GC run is performed, so GCed objects are never part of the snapshots. However with the "Record Heap Allocations" tool constantly all allocations are being recorded (that's why it may slow down your application a lot when it is recording). If you are experiencing frequent GC runs, this tool can help you identify places in your code where lots of memory is allocated. In conjunction with the Heap SnapShot comparison you will see that most of the time a lot more memory is allocated between two snapshots, than you can see from the comparison. In extreme cases the comparison will yield no difference at all, whereas the allocation tool will show you lots and lots of allocated memory (which obviously had to be garbage collected in the meantime).

Unfortunately the current version of the tool does not show you where the allocation took place, but it will show you what has been allocated and how it is was retained at the time of the allocation. From the data (and possibly the constructors) you will however be able to identify your objects and thus the place where they are being allocated.


If you're trying to choose between a few likely culprits, you could modify the object definition to attach themselves to the global scope (as list under document or something).Then this will stop them from being collected. Which may make the program faster (they're not being reclaimed) or slower (because they build up and get checked by the mark-and-sweep every time). So if you see a change in performance, you may have found the problem.

One alternative is to look at how many objects are being created of each type (set up a counter in the constructor). If they're getting collected a lot, they're also being created just as frequently.