`
cunzhangok
  • 浏览: 64915 次
  • 性别: Icon_minigender_1
  • 来自: 江苏
社区版块
存档分类
最新评论

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 = …;
分享到:
评论
1 楼 gufenglian 2012-12-18  
亲,只能静态化ChannelFactory规避是指什么?我也遇到这个问题

相关推荐

Global site tag (gtag.js) - Google Analytics