netty内存溢出OutOfDirectMemoryError: failed to allocate 16777216 byte(s) of direct memory (used: 2595225

1 篇文章 0 订阅

使用netty框架做客户端服务器,项目运行一段时间后发现如下异常:

  • 异常信息:OutOfDirectMemoryError: failed to allocate 16777216 byte(s) of direct memory (used: 251658240, max: 259522560)
15:01:14.034 [nioEventLoopGroup-2-1] WARN io.netty.channel.DefaultChannelPipeline - An exceptionCaught() event was fired, and it reached at the tail of the pipeline. It usually means the last handler in the pipeline did not handle the exception.
io.netty.util.internal.OutOfDirectMemoryError: failed to allocate 16777216 byte(s) of direct memory (used: 251658240, max: 259522560)
	at io.netty.util.internal.PlatformDependent.incrementMemoryCounter(PlatformDependent.java:754)
	at io.netty.util.internal.PlatformDependent.allocateDirectNoCleaner(PlatformDependent.java:709)
	at io.netty.buffer.PoolArena$DirectArena.allocateDirect(PoolArena.java:755)
	at io.netty.buffer.PoolArena$DirectArena.newChunk(PoolArena.java:731)
	at io.netty.buffer.PoolArena.allocateNormal(PoolArena.java:247)
	at io.netty.buffer.PoolArena.allocate(PoolArena.java:215)
	at io.netty.buffer.PoolArena.allocate(PoolArena.java:147)
	at io.netty.buffer.PooledByteBufAllocator.newDirectBuffer(PooledByteBufAllocator.java:356)
	at io.netty.buffer.AbstractByteBufAllocator.directBuffer(AbstractByteBufAllocator.java:187)
	at io.netty.buffer.AbstractByteBufAllocator.directBuffer(AbstractByteBufAllocator.java:178)
	at io.netty.buffer.AbstractByteBufAllocator.ioBuffer(AbstractByteBufAllocator.java:139)
	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:714)
	at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:650)
	at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:576)
	at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
	at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
	at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
	at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
	at java.lang.Thread.run(Thread.java:745)

  • 异常信息: ByteBuf.release() was not called before it’s garbage-collected
15:02:45.223 [nioEventLoopGroup-2-1] ERROR io.netty.util.ResourceLeakDetector - LEAK: ByteBuf.release() was not called before it's garbage-collected. See https://netty.io/wiki/reference-counted-objects.html for more information.
Recent access records: 
Created at:
	io.netty.buffer.PooledByteBufAllocator.newDirectBuffer(PooledByteBufAllocator.java:363)
	io.netty.buffer.AbstractByteBufAllocator.directBuffer(AbstractByteBufAllocator.java:187)
	io.netty.buffer.AbstractByteBufAllocator.directBuffer(AbstractByteBufAllocator.java:178)
	io.netty.buffer.AbstractByteBufAllocator.ioBuffer(AbstractByteBufAllocator.java:139)
	io.netty.channel.DefaultMaxMessagesRecvByteBufAllocator$MaxMessageHandle.allocate(DefaultMaxMessagesRecvByteBufAllocator.java:114)
	io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:147)
	io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:714)
	io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:650)
	io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:576)
	io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
	io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
	io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
	io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
	java.lang.Thread.run(Thread.java:745)

多次寻找资料无果,查看netty官网找到答案(摘自官网):

ByteBuf is a reference-counted object which has to be released explicitly via the release() method.

官网说明ByteBuf需要显示的调用release()方法进行释放,当调用write()writeAndFlush()时,也就是我们有响应数据时,该方法会帮我们调用release()方法。而我之前的代码,在收到消息后,并不是每条数据都会响应,大多都是只接收的,并且没有显示调用release()方法,最终导致gc问题。显示gc推荐使用ReferenceCountUtil.release(msg)write()方法底层也是用的这个方法)。

  • 注意:release()方法实际上是把ByteBuf对象的引用计数refCnt进行-1操作(retain()将+1)。refCnt默认为1,当为0时,资源将会被释放,再次调用将抛出IllegalReferenceCountException异常,因此不可随意重复调用。
  • 6
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
io.netty.util.internal.outofdirectmemoryerror: failed to allocate 16777216 b 是一个错误提示,意味着在使用 io.netty 这个库时,尝试分配 16,777,216 字节的直接内存时出现了错误。 出现这个错误通常是由于系统中的直接内存不足导致的。直接内存是一种通过 Native Memory 来直接分配内存的方式,与 Java 堆内存不同。由于直接内存不受 JVM 垃圾回收的控制,因此需要手动释放。 解决这个问题的方法有以下几种: 1. 增加直接内存的容量:可以通过修改 JVM 的启动参数,增加直接内存的最大容量。可以使用 -XX:MaxDirectMemorySize 参数指定最大直接内存大小。例如,可以设置为 -XX:MaxDirectMemorySize=1g 来将最大直接内存大小设置为 1GB。 2. 检查代码中的资源是否正确释放:如果在代码中使用了 io.netty 这个库,需要确保在使用完毕后正确释放资源。可以通过调用相关的 close() 或者 release() 方法来释放所占用的直接内存。 3. 调整程序的设计:如果程序中的网络请求或者数据处理量非常大,考虑对程序进行优化,减少对直接内存的依赖。可以通过合理缓存、分批处理等方式来减少直接内存的使用。 总之,当出现 io.netty.util.internal.outofdirectmemoryerror: failed to allocate 16777216 b 错误时,需要检查直接内存的容量是否足够,释放资源是否正确,以及是否需要对程序进行优化,减少对直接内存的依赖。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值