资源管理
目的
在处理数据的过程中,我们需要确保没有任何的资源泄漏。这时候我们就得很关心资源管理。
引用计数的处理
使用完ByteBuf之后,需要调整其引用计数以确保资源的释放
内存内漏探测
Netty提供了ResourceLeakDetector来检测内存泄漏,因为其是采样检测的,所以相关开销并不大。
- 泄露日志
- 检测级别
- 手动释放消息
ReferenceCountUtil.SafeRelease(this.Message);
- 分析SimpleChannelInboundHandler
public override void ChannelRead(IChannelHandlerContext ctx, object msg) { bool release = true; try { if (this.AcceptInboundMessage(msg)) { I imsg = (I)msg; this.ChannelRead0(ctx, imsg); } else { release = false; ctx.