Why is finalize wrong?
Alan Bateman
Alan.Bateman at oracle.com
Wed Sep 3 13:29:41 UTC 2014
On 03/09/2014 13:41, Peter Levart wrote:
>
> DirectByteBuffers rely on Cleaner(s), which are PhanthomReferences and
> are managed (invoked) by the ReferenceHandler thread directly.
> Finalizers are just enqueued by ReferenceHandler thread, but they are
> invoked by the special FinalizerThread. That's because Cleaner(s) are
> internal JDK mechanism, while finalizers invoke client code so they
> can stall the FinalizerThread. A bug in a client code can bring
> FinalizerThread to a halt.
>
Indeed and there is many a war story of badly written finalizers.
On direct buffers the recommendation has always been to cache those as
there is a significant cost to allocation (there is wording on this in
javadoc). Sometimes you can find code that repeatedly allocates, uses
once and discards and this can certainly cause performance issues (and
potentially OOME prior to your changes in this area).
-Alan
More information about the core-libs-dev
mailing list