公共美孚的getFoo(美孚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();
}
}
公共静态无效的主要(字串[] 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)| LEAK: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$2.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$2.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(AbstractChannelHandlerCont ext.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 $ 2.run(SingleThreadEventExecutor.java:116) io.netty.util .concurrent.DefaultThreadFactory $ DefaultRunnableDecorator.run(DefaultThreadFactory.java:137) java.lang.Thread.run(Thread.java:745)
您将需要添加更多信息...但我怀疑你错过了在管道中最后一个处理程序上的ByteBuf上调用释放。 – 2015-03-19 13:42:41
@NormanMaurer:我以为SimpleChannelInboundHandler假设释放msg ByteBuf。一旦我添加了msg.release();在channelRead0方法的最后,泄漏消失了。但是,SimpleChannelInboundHandler是否假设要照顾释放? – Paul 2015-03-19 18:29:17
它应该这样做。您可以添加自定义处理程序的代码 – 2015-03-20 05:41:41