netty 中的资源泄漏

Resource Leak in netty

public Foo getFoo(Foo f) 抛出 JsonProcessingException,InterruptedException {

    if (!clientChannel.isRegistered()) {
        System.out.println("Client is not registered");
        return null;
    }
    String s = mapper.writer().writeValueAsString(f);

    ByteBuf message = Unpooled.buffer(s.length() +8);
    message.writeInt(1);//Tag
    message.writeInt(s.length());//length
    message.writeBytes(s.getBytes());
    clientChannel.writeAndFlush(message);

    Foo response = handler.q.take();

    return response;
}    


public static class JsonClientHandler extends SimpleChannelInboundHandler<ByteBuf>{


    ArrayBlockingQueue<ApplicationRoute> q = new ArrayBlockingQueue<ApplicationRoute>(1);

    public JsonClientHandler() {
        super(false);
    }


    @Override
    public void channelRead0(ChannelHandlerContext ctx, ByteBuf msg)
            throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        byte[] a = new byte[msg.readableBytes()];
        msg.readBytes(a);
        Foo route = mapper.reader(Foo.class).readValue(a);
        q.add(route);
        //msg.release();

    }

}

public static void main(String[] args) {

        TestClient c = new TestClient();
        try {

            c.connect();
            Foo f = new Foo();
            for (int i =0; i < 1000; i++) {
                f = c.getFoo(f);
            }   
        }catch(Exception e) {
            e.printStackTrace();
        }
        finally {
            c.disconnect();
        }
   }

18/03/2015 14:17:43,811 nioEventLoopGroup-4-1 错误 io.netty.util.ResourceLeakDetector.reportLeak(ResourceLeakDetector.java:244) |泄漏:ByteBuf.release() 在被垃圾收集之前未被调用。 最近访问记录:2

2:

io.netty.buffer.AdvancedLeakAwareByteBuf.readBytes(AdvancedLeakAwareByteBuf.java:457)
com.foo.NettyClient$JsonClientHandler.channelRead0(NettyClient.java:208)
com.foo.NettyClient$JsonClientHandler.channelRead0(NettyClient.java:1)
io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105)
io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308)
io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294)
io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:182)
io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308)
io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294)
io.netty.handler.ssl.SslHandler.unwrap(SslHandler.java:1038)
io.netty.handler.ssl.SslHandler.decode(SslHandler.java:911)
io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:268)
io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:168)
io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308)
io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294)
io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:846)
io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:130)
io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:511)
io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:468)
io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:382)
io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354)
io.netty.util.concurrent.SingleThreadEventExecutor.run(SingleThreadEventExecutor.java:116)
io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:137)
java.lang.Thread.run(Thread.java:745)

1:

io.netty.buffer.AdvancedLeakAwareByteBuf.writeBytes(AdvancedLeakAwareByteBuf.java:559)
io.netty.handler.codec.LengthFieldBasedFrameDecoder.extractFrame(LengthFieldBasedFrameDecoder.java:495)
io.netty.handler.codec.LengthFieldBasedFrameDecoder.decode(LengthFieldBasedFrameDecoder.java:424)
io.netty.handler.codec.LengthFieldBasedFrameDecoder.decode(LengthFieldBasedFrameDecoder.java:343)
io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:268)
io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:168)
io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308)
io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294)
io.netty.handler.ssl.SslHandler.unwrap(SslHandler.java:1038)
io.netty.handler.ssl.SslHandler.decode(SslHandler.java:911)
io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:268)
io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:168)
io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308)
io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294)
io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:846)
io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:130)
io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:511)
io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:468)
io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:382)
io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354)
io.netty.util.concurrent.SingleThreadEventExecutor.run(SingleThreadEventExecutor.java:116)
io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:137)
java.lang.Thread.run(Thread.java:745)

创建于: io.netty.buffer.UnpooledByteBufAllocator.newDirectBuffer(UnpooledByteBufAllocator.java:55) io.netty.buffer.AbstractByteBufAllocator.directBuffer(AbstractByteBufAllocator.java:155) io.netty.buffer.AbstractByteBufAllocator.directBuffer(AbstractByteBufAllocator.java:146) io.netty.buffer.AbstractByteBufAllocator.buffer(AbstractByteBufAllocator.java:83) io.netty.handler.codec.LengthFieldBasedFrameDecoder.extractFrame(LengthFieldBasedFrameDecoder.java:494) io.netty.handler.codec.LengthFieldBasedFrameDecoder.decode(LengthFieldBasedFrameDecoder.java:424) io.netty.handler.codec.LengthFieldBasedFrameDecoder.decode(LengthFieldBasedFrameDecoder.java:343) io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:268) io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:168) io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308) io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294) io.netty.handler.ssl.SslHandler.unwrap(SslHandler.java:1038) io.netty.handler.ssl.SslHandler.decode(SslHandler.java:911) io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:268) io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:168) io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308) io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294) io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:846) io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:130) io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:511) io.netty.channel.nio.NioEventLoop.processSelectedKeys优化(NioEventLoop.java:468) io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:382) io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354) io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116) io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:137) java.lang.Thread.run(Thread.java:745)

一旦我添加了 msg.release();在 channelRead0 方法的最后,泄漏消失了。 感谢 Norman Maurer