Java GC - is there a way to determine which object

2019-04-07 05:06发布

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!

7条回答
淡お忘
2楼-- · 2019-04-07 05:36

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.

查看更多
Explosion°爆炸
3楼-- · 2019-04-07 05:40

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);
}
查看更多
趁早两清
4楼-- · 2019-04-07 05:40

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.

查看更多
在下西门庆
5楼-- · 2019-04-07 05:44

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.

查看更多
贼婆χ
6楼-- · 2019-04-07 05:44

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.

查看更多
戒情不戒烟
7楼-- · 2019-04-07 05:46

I haven't used this flag myself -XX:-TraceClassUnloading. It's meant to Trace unloading of classes.

查看更多
登录 后发表回答