我使用ChannelInboundHandlerAdapter类和writeAndFlush来处理来自channelRead()的每一条消息;
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf msgBuffer = (ByteBuf)msg;
BinaryWebSocketFrame frame= new BinaryWebSocketFrame(msgBuffer);
ctx.writeAndFlush(frame);
}在本例中: Netty发布了"frame“和"msg”吗?我知道它们是引用计数对象,因此,当我尝试手动释放它们时,如下所示:
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buf = (ByteBuf)msg;
BinaryWebSocketFrame frame= new BinaryWebSocketFrame(buf);
ctx.writeAndFlush(frame);
buf.release();
frame.release();
}但在这种情况下,我得到了refCounted对象发布错误,如下所示:
io.netty.util.IllegalReferenceCountException: refCnt: 0, decrement: 1起初,我依赖netty,它会释放相关实例,但偶尔会在运行时出现内存泄漏错误。
在这种情况下,如何减少内存使用,因为在每个channelRead事件中创建新实例并不是一个好主意,因为当您不能正确地释放它们时。
发布于 2017-05-01 13:39:36
是的,如果调用writeAndFlush(...),基本上就是转移缓冲区的所有权。如果写入缓冲区或写入失败,Netty本身将释放缓冲区。
https://stackoverflow.com/questions/43654924
复制相似问题