Dead Continuation cause resource leak?

施慧 kalinshi at qq.com
Thu May 28 06:42:25 UTC 2020


Hi All,


Trying to understand Loom continuation implementation. In following LeakTest,  Continuation Object is unreachable after first yield, but its runnable target is not finished yet.
There might be some resources allcoated during continuation run (native memory in this test case and free in finally block --- not cleaner way), when continuation object is collected, these resources are not closed or freed.


Is it possible to "clean up" a dead continuation which is not finished yet but collecting by GC? 


Tested with code cloned from github today.
javac --add-exports java.base/jdk.internal.ref=ALL-UNNAMED --add-exports java.base/jdk.internal.misc=ALL-UNNAMED  LeakTest.java
java --add-exports java.base/jdk.internal.ref=ALL-UNNAMED --add-exports java.base/jdk.internal.misc=ALL-UNNAMED  LeakTest
clean continuation



import jdk.internal.ref.Cleaner;
import jdk.internal.misc.Unsafe;
public class LeakTest {
    private static final Unsafe unsafe = Unsafe.getUnsafe();
    static ContinuationScope scope = new ContinuationScope("scope");
    public static void main(String[] args) throws Exception {
        bar();
        System.gc();
        Thread.sleep(1000);
        System.gc();
        Thread.sleep(1000);
    }


    public static void bar() {
        Continuation cont = new Continuation(scope, () -> {
            long mem = 0;
            try {
                // open file/socket
                mem = unsafe.allocateMemory(100);
                Continuation.yield(scope);
            } finally {
                unsafe.freeMemory(mem);
                System.out.println("release memory");
            }
        });
        Cleaner.create(cont, () -> { System.out.println("clean continuation");  });
        cont.run();
        //cont.run();
    }
}



Regards


More information about the loom-dev mailing list