Adi
Adi

Reputation: 57

How to track objects being cleared by JAVA GC in NetBeans?

How can I see the objects being cleared during a Java GC Cycle in NetbBeans ? Also is there a way to know when the GC is triggered when I am running my application ?

Thanks a lot for your responses and time.

Upvotes: 1

Views: 252

Answers (3)

JB-
JB-

Reputation: 2670

You can use PhantomReference - it is a way how to track objects eligible for being reclaimed without the performance penalty of finalization. Also, it is not possible to bring the phantom reachable objects back to live accidentally.

You can use code similar to this to achieve the goal:

public class GCTracker extends PhantomReference {
  private Object id;
  public GCTracker(Object object, ReferenceQueue referenceQueue, Object id) {
    super(object, referenceQueue);
    this.id = id;
  }
  public Object getId() {
    return id;
  }
}
ReferenceQueue<GCTracker> q = new ReferenceQueue<>();
Object id = "id1";
// never use the same object for the referent and the id
GCTracker pr = new GCTracker(object, referenceQueue, id);
// Later on another point
GCTracker r = q.remove();

// Use r.getId() to track the object being reclaimed

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533492

You can track a object by making it finalisable. Generally this is a bad idea but this is the simplest way.

You can detect a GC has occurred by looking at the MemoryManagerMXBean(s) for JMX.

NotificationFilter filter = new NotificationFilter() {
    @Override
    public boolean isNotificationEnabled(Notification notification) {
        return true;
    }
};
NotificationListener listener = new
        NotificationListener() {
            @Override
            public void handleNotification(Notification notification, Object handback) {
                System.out.println(notification);
            }
        };
for (MemoryManagerMXBean memoryManagerMXBean : ManagementFactory.getMemoryManagerMXBeans()) {
    if (memoryManagerMXBean instanceof NotificationBroadcaster)
        ((NotificationBroadcaster) memoryManagerMXBean).addNotificationListener(listener, filter, memoryManagerMXBean);
}
System.gc();
Thread.sleep(500);

prints

javax.management.Notification[source=java.lang:type=GarbageCollector,name=PS Scavenge][type=com.sun.management.gc.notification][message=PS Scavenge]
javax.management.Notification[source=java.lang:type=GarbageCollector,name=PS MarkSweep][type=com.sun.management.gc.notification][message=PS MarkSweep]

Obviously you wouldn't do either of these things unless you had a really good reason and it was really useful because they add complexity to your code and an overhead on your JVM.

Upvotes: 2

Giulio Franco
Giulio Franco

Reputation: 3230

Basically, no. You can declare a finalizer to know if they are not reachable anymore, but this does not mean they have actually been disposed. They may be freed some time in the future, or not be freed at all.

If you suspect that there is a memory leak in your application, there are tools specifically meant for this kind of analysis, and you can ask the Java VM to print detailed GC information (-XX:-PrintGCDetails). For more information about this, see http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html

Upvotes: 0

Related Questions