Java GC - is there a way to determine which objects collected
I'm trying to monitor the gc activity in my app, using -verbosegc flag. I can s开发者_运维百科ee there are full and minor collections, but is there a way to determine (subscrbing to events/vm flags/whatever) which objects actually collected?
Thanks!
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):
static ReferenceQueue<MyObject> MY_QUEUE = new ReferenceQueue<MyObject>();
static class MyReference extends WeakReference<MyObject>{
public final String name;
public MyReference(MyObject o, ReferenceQueue<MyObject> q){
super(o, q);
name = o.toString();
}
}
static{
Thread t = new Thread(){
public void run(){
while(true){
MyReference r = (MyReference)MY_QUEUE.remove();
System.out.println(r.name+" eligible for collection");
}
}
}
t.setDaemon(true);
t.start();
}
public MyObject(){
//normal init...
new MyReference(this, MY_QUEUE);
}
I haven't used this flag myself -XX:-TraceClassUnloading
. It's meant to Trace unloading of classes.
finalize method of the Object class is called just before the GC collects the object. Override the method in your class as follows:
@Override
protected void finalize() throws Throwable {
System.out.println(this+" collected");
super.finalize();
}
Note that you can only monitor your own classes with this method. So, since String is a final class, you cannot monitor a String object with this way.
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.
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.
精彩评论