上一篇讲过,收发的数据都会先放入内存,并且这个内存还会是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);
}
}