Netty异常总结及解决方法

原始链接: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次数少了很多

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Netty是一个基于Java的高性能网络编程框架,它提供了一种简单而强大的方式来处理网络通信。在Netty中,异常处理是非常重要的一部分,可以帮助我们及时发现和解决网络通信中的问题。下面是一个Netty异常处理的案例: 1. 异常捕获和处理: 在Netty中,可以通过实现ChannelHandler的exceptionCaught方法来捕获和处理异常。当有异常发生时,Netty会自动调用该方法,并将异常传递给它。在该方法中,我们可以根据具体的异常类型进行相应的处理,例如记录日志、关闭连接等。 ```java public class MyHandler extends ChannelInboundHandlerAdapter { @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { // 异常处理逻辑 if (cause instanceof IOException) { // 处理IO异常 // ... } else if (cause instanceof IllegalArgumentException) { // 处理参数异常 // ... } else { // 其他异常处理 // ... } // 关闭连接 ctx.close(); } } ``` 2. 异常传播: 在Netty中,异常可以在ChannelPipeline中传播。当一个ChannelHandler抛出异常时,它会被传递给ChannelPipeline中的下一个ChannelHandler,直到被处理或者到达Pipeline的末尾。这种机制可以让我们在不同的Handler中对异常进行处理,从而实现更加灵活的异常处理策略。 3. 异常日志记录: 在Netty中,可以使用日志框架来记录异常信息,以便后续的排查和分析。常用的日志框架有Log4j、Logback等。通过配置日志级别和输出格式,可以将异常信息记录到日志文件中,方便后续的查看和分析。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值