2.5 gigabytes of 4 g storage completed

I have read many articles on how the finalizer works. Here is my understanding: if the class has completed the implemented method, Jvm will create an instance of Finalizer as a guard dog on this object.

When the GC starts, it will mark the object to be deleted and add them to the reference queue, then the finalizer thread will select these objects from the queue and execute their finalization method.

My question is: how do I find an object from a heap dump whose finalize method was not finished for some reason and started accumulating a reference queue?

Is the link queue in a specific order?

+7
java finalizer
source share
2 answers

This may not be the answer you are looking for, but do you think you are using PhantomReference instead of overriding finalize() ? Here is an article that talks about this.

The basic idea is that it is not recommended to rely on the finalyze() method for preprocessing, since

  • You cannot predict when it will be called.
  • It uses JVM resources.
  • This may interfere with garbage collection.

PhantomReference provides a cleaner way to trigger an action when a garbage collector deletes an object.

 Object objectToHandle = new Object(); ReferenceQueue queue = new ReferenceQueue(); PhantomReference reference = new PhantomReference(objectToHandle, queue); 

When an objectToHandle is deleted from memory by the garbage collector, its reference will be added to the queue . You can detect this by calling queue.remove() and then do your cleanup.

 // will block until a reference becomes available Reference removedRef = queue.remove(); //.. you can now perform clean-up actions 

Note: PhantomReference.get() always returns null , so it is not possible to return an object after it has already been deleted from memory.

0
source share

Yes, you can get an object from a heap dump.

First, the ref finalizer will be removed from the queue in java.lang.ref.Finalizer.FinalizerThread#run , and then in runFinalizer it will be removed from the double-linked list of unfinalized links in the remove method.

But you can find this link because you know its GC root (it exists in the FinalizerThread stack)

How to find it in eclipse MAT:

  • Go to the histogram and filter by class java.lang.ref.Finalizer
  • right click on the row with finalizers -> Combine the shortest paths to the gc roots with all the links.
  • Expand the line with FinalizerThread (usually it should contain a single java.lang.Finalizer object)
  • Click on this extended line, and in the inspector panel you can click the attributes tab, the finalized object will be the referent enter image description here
0
source share

All Articles