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 ERROR io.netty.util.ResourceLeakDetector.reportLeak(ResourceLeakDetector.java:244)|泄漏:在垃圾收集之前没有调用ByteBuf.release()。 最近的访问记录: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)
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(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)
答案 0 :(得分:0)
我添加了msg.release();在channelRead0方法的最后,泄漏消失了。 感谢Norman Maurer