I'm trying to monitor the gc activity in my app, using -verbosegc flag. I can see there are full and minor collections, but is there a way to determine (subscrbing to events/vm flags/whatever) which objects actually collected?
Thanks!
I'm trying to monitor the gc activity in my app, using -verbosegc flag. I can see there are full and minor collections, but is there a way to determine (subscrbing to events/vm flags/whatever) which objects actually collected?
Thanks!
I know this isn't how you'd like to solve your problem, but it might be useful anyway.
You can capture the event on your own objects by overriding the finalize method. It doesn't 100% guarantee that the object will be Garbage Collected since it can create references to itself, but it's a start.
Take a look at this article it's a pretty good GC Tutorial.
For general information about objects in memory I would suggest you look into jvisualvm (it is in the bin folder of your JDK). It has alot of useful information about what the VM is doing as your program runs, including information about the various objects, and memory state.
If you want something more specific you can use WeakReferences and ReferenceQueues. This option might be viable if you are only interested in objects of a few type. You can create a WeakReference to the objects as they are created with a common ReferenceQueue, and then have another thread check the Queue periodically (note that the queue only says the objects are reachable, not that they are actually collected):
I'm not entirely sure why you need to know this. Most people want to know this to determine if they have memory leaks. (In java that means that you keep an object alive by keeping a reference to an object).
Netbeans has great tools to look at the memory use of any java application (also the ones not running from netbeans!) They can tell you how many objects have been collected and where your memory usage is going, and many more useful statistics.
To analyze memory problems you need to check which objects are not GCed, instead of checking which objects got GCed.
To check which objects are GCed you can always use any profiler like Jprofiler etc.
In general you cannot determine which object is reclaimed. You can however find get a subset of it, but you have to reference them using Weak, Soft and Phantom reference. In general what you do is create an object then reference it using one of those reference. See this article.
I haven't used this flag myself
-XX:-TraceClassUnloading
. It's meant to Trace unloading of classes.