tomp2p / TomP2P

A P2P-based high performance key-value pair storage library
http://tomp2p.net
Apache License 2.0
438 stars 122 forks source link

PeerConnection Listener Memory Leak? #135

Open cal2195 opened 8 years ago

cal2195 commented 8 years ago

Hello,

I've been working with this library, and I've come across a problem. While using the PeerDHT, after a few hours, my application crashes with OutOfMemory. It appears that the PeerConnections aren't being GC'd, and they build up after a couple of hours!

After inspecting the heap dump, I can see a NioDatagramChannel with 390,509 closeFuture listeners attached, most of which are PeerConnections?

Am I doing something wrong, or is there a reason why tens of thousands of PeerConnections are persisting between 4 peers?

Thanks!

cal2195 commented 8 years ago

After some testing, removing

private void addCloseListener(final ChannelFuture channelFuture) {
        channelFuture.channel().closeFuture().addListener(new GenericFutureListener<Future<? super Void>>() {
            @Override
            public void operationComplete(Future<? super Void> arg0) throws Exception {
                LOG.debug("About to close the connection {}, {}.",  channelFuture.channel(), initiator ? "initiator" : "from-disptacher");
                closeFuture.done();
            }
        });
    }

from PeerConnection.java seems to have stopped the problem. Is there a good way of preventing this memory leak other than this?