ChannelInboundHandlerAdapter writeAndFlush(msg)方法在刷新后是否释放msg? [英] ChannelInboundHandlerAdapter writeAndFlush(msg) method does release msg after flushing?

查看:153
本文介绍了ChannelInboundHandlerAdapter writeAndFlush(msg)方法在刷新后是否释放msg?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ChannelInboundHandlerAdapter类并对来自channelRead()的每条消息进行writeAndFlush;

I am using ChannelInboundHandlerAdapter class and writeAndFlush every message that came from 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".我知道它们是引用计数的对象,因此当我尝试手动释放它们时,如下所示:

In this case: Does Netty release "frame" and the "msg". I know that they are reference counted object, thus when I try to release them manually like the following:

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对象释放错误,如下所示:

But in that case I am getting refCounted object release error which is the following:

io.netty.util.IllegalReferenceCountException: refCnt: 0, decrement: 1

起初,我依靠netty释放相关实例,但偶尔会在运行时给我带来内存泄漏错误.

At first, I relied on netty that it releases the related instances but it gave me memory leak error on runtime occasionally.

在这种情况下如何减少内存使用,因为在无法正确释放它们的情况下,在每个channelRead事件中创建新实例并不是一个好主意.

How to reduce memory usage in such case because creating new instances in every channelRead event is not good idea when you can not release them properly.

推荐答案

是的,如果您调用 writeAndFlush(...),则基本上是在转移缓冲区的所有权.如果写入或写入失败,Netty本身将释放缓冲区.

Yeah if you call writeAndFlush(...) you are basically transfer the ownership of the buffer. Netty itself will release the buffer if it was written or if the write fails.

这篇关于ChannelInboundHandlerAdapter writeAndFlush(msg)方法在刷新后是否释放msg?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆