java - 由于 Netty 中的 ByteBuffers 导致的内存泄漏
问题描述
我创建了一个小型 Netty 服务器来计算 BigInteger 的阶乘并发送结果。代码如下。
阶乘.java
public class Factorial {
private int port;
public Factorial(int port) {
this.port = port;
}
public void run(int threadcount) throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup(threadcount);
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new FactorialHandler());
}
})
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture f = b.bind(port).sync();
f.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception {
int port = 15000;
new Factorial(port).run(Integer.parseInt(args[0]));
}
}
FactorialHandler.java
public class FactorialHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
BigInteger result = BigInteger.ONE;
String resultString;
for (int i=2000; i>0; i--)
result = result.multiply(BigInteger.valueOf(i));
resultString = result.toString().substring(0, 3)+"\n";
ByteBuf buf = Unpooled.copiedBuffer(resultString.getBytes());
ctx.write(buf);
ctx.flush();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
cause.printStackTrace();
ctx.close();
}
}
当我运行它时,出现以下错误
Jun 08, 2018 5:28:09 PM io.netty.util.ResourceLeakDetector reportTracedLeak
SEVERE: LEAK: ByteBuf.release() was not called before it's garbage-collected. See http://netty.io/wiki/reference-counted-objects.html for more information.
Recent access records:
正如给定链接中所解释的,我通过buf.release()
在之后调用 channelRead 方法释放了 ByteBuffer ctx.flush()
。
但是当我这样做时,服务器开始抛出以下异常
io.netty.util.IllegalReferenceCountException: refCnt: 0, increment: 1
有人可以告诉我如何解决这个问题吗?
解决方案
问题不在于出站 ByteBuf。出站 ByteBuf 始终为您处理(请参阅OutboundMessages)。问题是入站 ByteBuf。我在看着你,FactorialHandler。它扩展了 ChannelInboundHandlerAdapter。请注意 JavaDoc 中的这一点:
请注意,channelRead(ChannelHandlerContext, Object) 方法自动返回后不会释放消息。如果您正在寻找自动释放接收到的消息的 ChannelInboundHandler 实现,请参阅SimpleChannelInboundHandler。
您的处理程序具有如下签名:
public void channelRead(ChannelHandlerContext ctx, Object msg)
该消息(顺便说一句,您不使用)实际上是一个 ByteBuf,这正是上面的 JavaDoc 注释警告您的内容。(在没有任何其他 ChannelHandler 的情况下,消息将始终是 ByteBuf 的实例。)
所以你的选择是:
- 使用 SimpleChannelInboundHandler 它将为您清理该引用。
- 在处理程序结束时,使用ReferenceCountUtil.release(java.lang.Object msg)释放入站 ByteBuf 。
推荐阅读
- linux - CI Gitlab fails with docker daemon system failure
- java - Spring AOP 不适用于小功能
- python - 一直在 sqlite python 中启用自动提交是一个好习惯吗?
- php - Laravel中数据透视表的访问关系
- python - Select2 and django_filters not querying foreign keys
- azure-container-service - How to pass Azure Storage connection string to Container Instance
- php - memcached:解析 session.save_path 失败
- javascript - POST 方法后清除所选值
- c++ - MS Word InsertBreak 抛出异常
- javascript - 生成具有以下条件的数字序列