Caused by: java.lang.OutOfMemoryError: Direct buffer memory

来自:http://code.alibabatech.com/blog/experience_1348/netty-nio-direct-memory-leak.html

在测试中发现,当不停的开关Netty的NioClientSocketChannelFactory(比如大量连接失败重连等情况下),存在Direct Memory泄露。
测试代码:

for (int i = 0; i < Integer.MAX_VALUE; i ++) {
ChannelFactory channelFactory = new NioClientSocketChannelFactory(
Executors.newCachedThreadPool(), Executors.newCachedThreadPool());
Bootstrap bootstrap = new ClientBootstrap(channelFactory);
bootstrap.setPipelineFactory(new ChannelPipelineFactory() { … });
ChannelFuture future = bootstrap.connect(serverAddress);
future.await(); // or future.addListener(new ChannelFutureListener() { … });
Channel channel = future.getChannel();
channel.close();
// FIXME NioClientSocketChannelFactory direct buffer memory leak
channelFactory.releaseExternalResources(); // or bootstrap.releaseExternalResources();
}

抛出的异常信息:

Caused by: java.lang.OutOfMemoryError: Direct buffer memory
at java.nio.Bits.reserveMemory(Bits.java:633)
at java.nio.DirectByteBuffer.(DirectByteBuffer.java:95)
at java.nio.ByteBuffer.allocateDirect(ByteBuffer.java:288)
at org.jboss.netty.channel.socket.nio.SocketSendBufferPool$Preallocation.(SocketSendBufferPool.java:159)
at org.jboss.netty.channel.socket.nio.SocketSendBufferPool.(SocketSendBufferPool.java:46)
at org.jboss.netty.channel.socket.nio.NioWorker.(NioWorker.java:84)
at org.jboss.netty.channel.socket.nio.NioClientSocketPipelineSink.(NioClientSocketPipelineSink.java:74)
at org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory.(NioClientSocketChannelFactory.java:135)

询问了Netty的作者Trustin Lee:

ChannelFactories are never meant to be created many times. JVM is poor at managing direct buffers, so there’s no way to fix this problem without using JNI. One possible workaround would be call System.gc() explicitly, but I’m not sure it’s a reliable workaround.

看起来是JVM对direct buffer管理的问题,Netty估计也没有直接的办法解决,
只能静态化ChannelFactory规避,以减少泄露:

private static final NioClientSocketChannelFactory channelFactory = …;
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值