Netty异常总结及解决方法

本文总结了Netty在高并发下CPU使用率达到100%的问题及解决办法,即设置系统属性`org.jboss.netty.epollBugWorkaround`为`true`。此外,还探讨了因直接内存溢出导致的异常,提出了解决方案,包括手动释放内存和调整JVM参数。同时,提到了Netty不同版本对GC的影响。
摘要由CSDN通过智能技术生成

原始链接:Netty异常总结及解决方法 | 於之博客

1、netty网关启动之后,当并发8000访问网关时,cpu使用率达到了100%!且CPU温度报警

网上查找到了同样的问题:

This what jstack showed on Windows 2012 R2, however under ubuntu 12 it's much alike (if 100% the same) as stated here github.com/netty/netty/issues/327 The question is can I setup systemproperty right in the code like this? public static void main(String[] args) throws Exception { System.setProperty("org.jboss.netty.epollBugWorkaround", "true"); • You should be able to set the property in code, but it probably needs to be done as early as possible, before any Netty classes are loaded. First line in main() is probably easiest.

解决方案:在程序初始化完成之后,添加如下代码: System.setProperty("org.jboss.netty.epollBugWorkaround", "true");

2.当网关一直运行之后,直接内存溢出,异常:

io.netty.util.internal.OutOfDirectMemoryError: failed to allocate 16777216 byte(s) of direct memory (used: 1023410176, max: 1037959168)
	at io.netty.util.internal.PlatformDependent.incrementMemoryCounter(PlatformDependent.java:640)
	at io.netty.util.internal.PlatformDependent.allocateDirectNoCleaner(PlatformDependent.java:594)
	at io.netty.buffer.PoolArena$DirectArena.allocateDirect(PoolArena.java:764)
	at io.netty.buffer.PoolArena$DirectArena.newChunk(PoolArena.java:740)
	at io.netty.buffer.PoolArena.allocateNormal(PoolArena.java:244)
	at io.netty.buffer.PoolArena.allocate(PoolArena.java:214)
	at io.netty.buffer.PoolArena.allocate(PoolArena.java:146)
	at io.netty.buffer.PooledByteBufAllocator.newDirectBuffer(PooledByteBufAllocator.java:324)
	at io.netty.buffer.AbstractByteBufAllocator.directBuffer(AbstractByteBufAllocator.java:185)
	at io.netty.buffer.AbstractByteBufAllocator.directBuffer(AbstractByteBufAllocator.java:176)
	at io.netty.buffer.AbstractByteBufAllocator.ioBuffer(AbstractByteBufAllocator.java:137)
	at io.netty.channel.DefaultMaxMessagesRecvByteBufAllocator$MaxMessageHandle.allocate(DefaultMaxMessagesRecvByteBufAllocator.java:114)
	at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:147)
	at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:647)
	at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:582)
	at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:499)
	at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:461)
	at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:884)
	at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
	at java.lang.Thread.run(Thread.java:745)

程序占用资源分析:

解决方案:

  • 1.自己手动分配的直接内存需要自己手动释放(后文有详细说明) ReferenceCountUtil.release(buf)

  • 2.jvm调优 网关参数: -Xms1024m -Xmx1024m -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -Dio.netty.leakDetectionLevel=advanced -Xloggc:d:/gc.log -XX:MaxDirectMemorySize=1G -Dio.netty.allocator.pageSize=8192 -Dio.netty.allocator.maxOrder=10 -Dio.netty.recycler.maxCapacity=0 -Dio.netty.recycler.maxCapacity.default=0

Linux中使用option去设置系统参数,如jvm参数等,可用使用System.getProperty("a")获取到值;args通过main方法的参数捕获: java [-options] -jar jarfile [args...]

3.Netty版本对GC的影响:

4.1.6版本的netty 512M内存启动时:升级到4.1.16时 512M内存GC次数少了很多

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值