ChannelInboundHandlerAdapter writeAndFlush(msg) 方法是否在刷新后释放 msg?
ChannelInboundHandlerAdapter writeAndFlush(msg) method does release msg after flushing?
我正在使用 ChannelInboundHandlerAdapter class 和 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 事件中创建新实例在您无法正确释放它们时不是一个好主意。
是的,如果您调用 writeAndFlush(...)
,您基本上是在转移缓冲区的所有权。 Netty 本身会在写入缓冲区或者写入失败时释放缓冲区。
我正在使用 ChannelInboundHandlerAdapter class 和 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 事件中创建新实例在您无法正确释放它们时不是一个好主意。
是的,如果您调用 writeAndFlush(...)
,您基本上是在转移缓冲区的所有权。 Netty 本身会在写入缓冲区或者写入失败时释放缓冲区。