Memory leak in AsynchronousFileChannel?

Zhong Yu zhong.j.yu at gmail.com
Thu Apr 24 06:32:46 UTC 2014


AsynchronousFileChannel.open() with default executor spawns some
threads that never exit:

   java.lang.Thread.State: RUNNABLE
    at sun.nio.ch.Iocp.getQueuedCompletionStatus(Native Method)
    at sun.nio.ch.Iocp.access$300(Iocp.java:46)
    at sun.nio.ch.Iocp$EventHandlerTask.run(Iocp.java:333)
    at sun.nio.ch.AsynchronousChannelGroupImpl$1.run(AsynchronousChannelGroupImpl.java:112)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:744)

(win7, jdk8)

These threads reference all class loaders on the calling stack of
open(), through
Thread.inheritedAccessControlContext.context[*].classloader, which
prevents user class loaders from being garbage collected.

On the other hand, WatchService threads do not have this problem. I
guess it's due to doPrivileged():

sun.nio.fs.AbstractPoller

    public void start() {
        final Runnable thisRunnable = this;
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
            @Override
            public Object run() {
                Thread thr = new Thread(thisRunnable);
    ...


Regards,
Zhong Yu


More information about the nio-dev mailing list