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次数少了很多