栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

java netty 内存泄露

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java netty 内存泄露

偶然一次再测试环境看到netty出现内存泄漏的日志,报错信息如下:

LEAK: ByteBuf.release() was not called before it's garbage-collected. Enable advanced leak reporting to find out where the leak occurred. To enable advanced leak reporting, specify the JVM option '-Dio.netty.leakDetectionLevel=advanced' or call ResourceLeakDetector.setLevel()

看到这个错误,看不出内存泄漏的位置。但在环境里也没有设置

-Dio.netty.leakDetectionLevel=advanced

这个问题在测试环境出现还好,如果在生产环境那就糟糕了。所以怎么可以今早在开发过程中就发现以及定位到内存泄漏的问题呢?

Netty的内存泄漏检测分为四个级别:

DISABLED - 完成禁止检测内存泄漏,这个是不推荐。

SIMPLE - 如果buffer中出现1%的内存泄漏,打印错误日志,就是上面那样。但是日志是看不到详细在什么位置出现了内存泄漏。鉴于性能考虑,这个是在生产环境中默认使用。

ADVANCED - 如果buffer的1%出现内存泄漏,打印错误,并且输出详细的内存泄漏信息。

PARANOID - 这个和ADVANCED输出的内容是一样的,但它会对每一次请求的buffer做检查。很适用于调试和单元测试。

建议

1、为了今早的发现问题,强烈建议在开发调试过程中,把检测级别设为PARANOID,设置:

io.netty.leakDetection.level=PARANOID

2、谨记哪里消费buffer,哪里释放,即调用ReferenceCountUtil.release(msg)

public void channelRead(ChannelHandlerContext ctx, Object msg) {

try {

...

} finally {

ReferenceCountUtil.release(msg);

}

}

输出的详细信息示例

可以看出在EchoServerHandler上出现了内存泄漏。

12:05:24.374 [nioEventLoop-1-1] ERROR io.netty.util.ResourceLeakDetector - LEAK: ByteBuf.release() was not called before it's garbage-collected.

Recent access records: 2

#2:

Hint: 'EchoServerHandler#0' will handle the message from this point.

io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:329)

io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:846)

io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:133)

io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:485)

io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:452)

io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:346)

io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:794)

java.lang.Thread.run(Thread.java:744)

#1:

io.netty.buffer.AdvancedLeakAwareByteBuf.writeBytes(AdvancedLeakAwareByteBuf.java:589)

io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:208)

io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:125)

io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:485)

io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:452)

io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:346)

io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:794)

java.lang.Thread.run(Thread.java:744)

Created at:

io.netty.buffer.UnpooledByteBufAllocator.newDirectBuffer(UnpooledByteBufAllocator.java:55)

io.netty.buffer.AbstractByteBufAllocator.directBuffer(AbstractByteBufAllocator.java:155)

io.netty.buffer.AbstractByteBufAllocator.directBuffer(AbstractByteBufAllocator.java:146)

io.netty.buffer.AbstractByteBufAllocator.ioBuffer(AbstractByteBufAllocator.java:107)

io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:123)

io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:485)

io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:452)

io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:346)

io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:794)

java.lang.Thread.run(Thread.java:744)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/910211.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号