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

Luke Hutchison luke.hutch at gmail.com
Wed Oct 10 10:56:22 UTC 2018


On Wed, Oct 10, 2018 at 4:37 AM Luke Hutchison <luke.hutch at gmail.com> wrote:

> Separate question: other than using "synchronized (this)" around every
> method (including the finalizer), is there a way to emulate
> reachabilityFence in JDK 7 and 8?
>

For examle, I just tested this, and it seems to prevent the finalizer from
being run early (I create a local static instance FENCE of type Fence to
try to avoid Hotspot optimizing away the empty method
Fence::reachabilityFence) -- but is there a reason this could fail with
future Hotspot optimizations?:

=========================================

    private static class Fence {
        public void reachabilityFence(Object object) {
        }
    }

    private static Fence FENCE = new Fence();

    private Object fencedField;

    private void fencedMethod() {
        try {
            // ...
        } finally {
            FENCE.reachabilityFence(this);
            // Or maybe: FENCE.reachabilityFence(this.fencedField);
        }
    }


More information about the hotspot-dev mailing list