Finalizer being run while class still in use (escape analysis bug)

Luke Hutchison luke.hutch at gmail.com
Wed Oct 10 02:26:46 UTC 2018


On Tue, Oct 9, 2018 at 6:35 PM David Holmes <david.holmes at oracle.com> wrote:

> Also note that finalizers can run while methods of a class instance are
> still in progress [1]
>
> "Optimizing transformations of a program can be designed that reduce the
> number of objects that are reachable to be less than those which would
> naively be considered reachable."
>
>   - this is one of the evil things about finalizers. So it may not be a
> bug, just surprising.
>
> [1]
> https://docs.oracle.com/javase/specs/jls/se11/html/jls-12.html#jls-12.6
>

I would assume this is referring to the *visibility* of a symbol -- i.e. if
a symbol is in scope, the object referenced by that symbol is considered
reachable -- but aggressive escape analysis could schedule an object for
garbage collection after the last usage of the symbol within its defining
scope.

However, it should never be true that an object should be deemed
non-reachable when it is serving as the invocation target for a
currently-executing non-static method, since "this" is always reachable in
the scope of that non-static method.

I think this is what Martin was indicating with this comment:

On Tue, Oct 9, 2018 at 6:51 PM Martin Buchholz <martinrb at google.com> wrote:

> Search for calls to Reference.reachabilityFence(this); in the jdk sources.
>

In the JLS page you linked, you will also see:

> A reachable object is any object that can be accessed in any potential
continuing computation from any live thread.
> A finalizer-reachable object can be reached from some finalizable object
through some chain of references, but not from any live thread.

Since a live thread is still executing a method of the ScanResult object
when the finalizer is run, it cannot be true that this is simply "not a
bug, just suprising".

This is the whole intent of classes that override the finalize() method
being marked "GlobalEscape", so that escape analysis is disabled for these
classes, in order to prevent this exact thing from happening.


More information about the hotspot-dev mailing list