【发布时间】:2018-07-09 10:53:39
【问题描述】:
除非使用SimpleChannelInboundHandler channelRead0,否则如果ctx.fireChannelRead 未被调用,则应释放输入数据包。
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
log.error("receive {}", msg);
if (msg instanceof FullHttpRequest) {
FullHttpRequest req = (FullHttpRequest) msg;
ReferenceCountUtil.release(msg);
clientChannel.writeAndFlush(new RequestWrapper(req, foo));
}
}
clientChannel.writeAndFlush 成功时,将requestWrapper 推入队列。 netty 不显示LEAK warnings,但如jvm out of memory 中所述,该项目的Old Gen 增加,事件与ReferenceCountUtil.release(msg)。
- 如果http输入消息没有释放,为什么official example没有显式调用释放?
- 在
channelRead中,如果接收到的 msg 设置在另一个 bean 中,然后这个 bean 由ctx.fireChannelRead传递,我是否应该为这个 msg 调用 release,就像上面的代码一样? - 如果
ctx.fireChannelRead(newObject)传递了一个新对象,我应该在下一个处理程序中调用release(newObject)吗?
像这样:
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ctx.fireChannelRead("hello world");
}
public void channelRead(ChannelHandlerContext ctx, Object msg) {
assert "hello world".equals(msg);
ReferenceCountUtil.release(msg); // is this necessary if it is created in the former handler?
}
- 写操作怎么样,写对象也要调用release吗?
像这样:
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
ctx.write("bye", promise);
}
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
assert "bye".equals(msg);
// should I call ReferenceCountUtil.release(msg); ?
ctx.write(“bye bye”, promise);
}
【问题讨论】: