Netty (4)-内存回收和SimpleChannelInboundHandler

上一篇讲过,收发的数据都会先放入内存,并且这个内存还会是JVM以外的直接内存,所以需要我们手动去回收。

回收接收数据

如下,msg和in是两个不同的变量,但是都引用同一个对象,回收in或msg效果是一样的。

    public void channelRead(ChannelHandlerContext ctx, Object msg)  {
        try {
           ByteBuf in = (ByteBuf) msg;
           //业务处理
        } finally {
           ReferenceCountUtil.release(msg);    //回收
        }

回收发出数据

发出数据时,需要调用flush,这个方法会自动帮你回收发出的数据,所以不用手动回收。

SimpleChannelInboundHandler

接收的数据也可以自动回收,只需用SimpleChannelInboundHandler替代ChannelInboundHandlerAdapter,当执行完channelRead0后,msg就会被回收。

public class NettyServerHandler extends SimpleChannelInboundHandler<Object> {
	protected void channelRead0(ChannelHandlerContext ctx, Object msg)  {
	}

可以看下SimpleChannelInboundHandler的部分源码,接收消息时首先会调用它的channelRead,然后再调用你的channelRead0,最后它帮你实现了回收。关于if块的内容,是检查消息类型是否匹配,不匹配就交给下一个Handler去处理,以后再介绍。

public abstract class SimpleChannelInboundHandler<I> extends ChannelInboundHandlerAdapter {
    .....
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        boolean release = true;
        try {
            if (acceptInboundMessage(msg)) {
                @SuppressWarnings("unchecked")
                I imsg = (I) msg;
                channelRead0(ctx, imsg);
            } else {
                release = false;
                ctx.fireChannelRead(msg);
            }
        } finally {
            if (autoRelease && release) {
                ReferenceCountUtil.release(msg);
            }
        }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值