netty(九) http server 内存泄漏

[ERROR] 2018-09-13 10:57:45 523 [io.netty.util.ResourceLeakDetector] [nioEventLoopGroup-5-1] (Slf4JLogger.java:171) -> LEAK: ByteBuf.release() was not called before it's garbage-collected. See http://netty.io/wiki/reference-counted-objects.html for more information.

只出现一次 

参考:

http://calvin1978.blogcn.com/articles/netty-leak.html

https://www.jianshu.com/p/f3b3167cf6fc

加上-Dio.netty.leakDetectionLevel=advanced再跑,出现(这个不总是出现,即使用最高等级):

com.jds.httpserver.RequestToBodyDecoder.channelRead0(RequestToBodyDecoder.java:31)
com.jds.httpserver.RequestToBodyDecoder.channelRead0(RequestToBodyDecoder.java:22)

 文件:链接: https://pan.baidu.com/s/1YDpKH028p2f90Lom3vMLGw 密码: pjxg

https://www.cnblogs.com/silyvin/articles/9567157.html这个实践中

        ByteBuf msg = fullHttpRequest.content().retain();

 
        byte[] bs = new byte[msg.readableBytes()];
        msg.readBytes(bs);
        String json = new String(bs);
                ...
 
        super.channelRead(channelHandlerContext, jsonObject);

 

这一段是Simple~中的代码,retain只有,fullHttpRequest年龄+1,read0之后,-1,

而这个对象不再往下传递了,被传递的是json, 所以最后还是1,泄漏

改为:    ByteBuf msg = fullHttpRequest.content(),  经过实践认为再无发生此类内存泄漏

 

https://blog.csdn.net/u012807459/article/details/77259869

其释放的是channelRead传入的ByteBuf,如果在handlers传递过程中,传递了新值,老值需要你自己手动释放。

另外如果中途没有使用fireChannelRead传递下去也要自己释放。

在传递过程中自己通过Channel或ChannelHandlerContext创建的但是没有传递下去的ByteBuf也要手动释放。

Simple~, MessageToMesageDecoder/Encoder会自动释放,如果要传递,且传递的对象 msg instanceof ReferenceCounted,则要retain

posted on 2018-09-13 16:49  silyvin  阅读(494)  评论(0)    收藏  举报