About the finalization queue and reference queue

Jun Zhuang jun.zhuang at hobsons.com
Tue Oct 11 20:55:54 UTC 2016


Hi Bernd,



Appreciate your quick response. I understand following:

*         A Finalizer instance is created for every finalizeable object

*         All the Finalizer instances are linked together using a double linked list

*         All the Finalizer instances are tracked by the java.lang.ref.Finalizer class. Or is it only the first one by the unfinalized field?



What I am still not clear are:

1.       Is there a finalization queue at all? If so, what does it do?

2.       What goes into the ReferenceQueue? The finalizeable objects?



Thanks a lot,

Jun





-----Original Message-----
From: Bernd Eckenfels [mailto:ecki at zusammenkunft.net]
Sent: Tuesday, October 11, 2016 3:06 PM
To: hotspot-gc-use at openjdk.java.net
Cc: Jun Zhuang <jun.zhuang at hobsons.com>
Subject: Re: About the finalization queue and reference queue



Hello,



what is interesting to know is, that each finalizeable object which is tracked is wrapped/tracked with an instance of j.lang.ref.Finalizer (which is a FinalizerReference<Obj> subclass i.e. a final reference).



Generally for references to work you need to keep alive the Reference instance. The finalizer does this with a built-in linked list in the Finalizer instance (static unfinalized points to the head of the list and each Finalizer object has a next/prev pointer. When the VM tracks a finalizeable object it calls the Finalize(Object) constructor which makes sure to add it.



So if you have thousands of finalized objects there are all indirectly referenced from this long linear list.



When the GC does its thing and a instance becomes unreachable, it will add it to the ReferenceQueue of the finalizer. The FinalizerThread will consume it from there and make sure to finally also remove the Finalizer reference for that object from the double linked list.



If you work with heap-dumps and analyse memeory leak informations you need to ignore the memeory consumotion under "Finalizer.unfinalized"

with the (possibly long if the Finalizer thread is blocked and unwanted) ReferenceQueue Finalizer#queue#head single linked list.



Gruss

Bernd



Am Tue, 11 Oct 2016 18:25:29 +0000

schrieb Jun Zhuang <jun.zhuang at hobsons.com<mailto:jun.zhuang at hobsons.com>>:



> Hi,

>

> While reading about re-defining the finalize() method explicitly in a

> class I came across some statements and like to get some clarification

> from the experts.

>

> On http://www.fasterj.com/articles/finalizer2.shtml, the author states

> that "the GC adds each of those Finalizer objects to the reference

> queue at java.lang.ref.Finalizer.ReferenceQueue.". Based on this the

> Finalizer object associated with the finalizeable object goes on the

> reference queue.

>

> On page 311 of book Service-Oriented Computing - ICSOC 2011

> Workshops<https://books.google.com/books?id=hIa5BQAAQBAJ&pg=PA311&lpg=

> PA311&dq=java,+finalization+queue&source=bl&ots=LLGiYGWh0L&sig=Glvf0kn

> 0zKHrdWoPzM6y6wtsr_M&hl=en&sa=X&ved=0ahUKEwjHrsL2pdPPAhUk3IMKHdXRCtc4C

> hDoAQgbMAA#v=onepage&q=finalization%20queue&f=false>

> "... all those objects that have a finalize () method and are found to

> be unreachable(dead) by garbage collector, are pushed into a

> finalization queue.". So the finalizeable object goes on the

> finalization queue.

>

> Then this site, https://yourkit.com/forum/viewtopic.php?f=3&t=4672,

> states that "Objects of all classes with redefined finalize() method

> are added to a queue at the moment of creation. The queue head is

> referenced from a static field in java.lang.ref.Finalizer. An instance

> of Finalizer is created for each "finalizeable" object and is stored

> in that queue, which is in fact a linked list of Finalizers.", so both

> the finalizeable object and the associated Finalizer object are stored

> in the same queue?

>

> So my questions are: Are there one or two queues involved? Exactly how

> object finalization works?

>

>

> Appreciate your input,

> Jun

>

> Jun Zhuang

> Sr. Performance QA Engineer |

> Hobsons<https://www.hobsons.com/?utm_source=outlook&utm_medium=email&u

> tm_campaign=banner_02.12.16_general>

> T: +1 513 746 2288 | jun.zhuang at hobsons.com<mailto:jun.zhuang at hobsons.com> 50 E-Business Way, Suite

> 300 | Cincinnati, OH 45241 | USA

>

>

> Upgraded by Hobsons - Subscribe Today

>


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/hotspot-gc-use/attachments/20161011/89131731/attachment.html>


More information about the hotspot-gc-use mailing list