来自: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 = …;
分享到:
相关推荐
Caused by: java.lang.OutOfMemoryError: PermGen space解决方案, avax.servlet.ServletException: java.lang.OutOfMemoryError: PermGen space at org.apache.jasper.servlet.JspServlet.service(JspServlet.java...
Caused by: java.net.UnknownHostException: openapi.alipay.com
整个系统都没有错误,但是在发布运行后,控制台上却抛出了这样的异常: java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource
NULL 博文链接:https://geyubin.iteye.com/blog/779330
Android Caused by: java.lang.ClassNotFoundException解决办法 出现问题: 08-13 18:29:22.924: E/AndroidRuntime(1875): Caused by: Java.lang.ClassNotFoundException:XXXXX in loader dalvik.system....
java解决nested exception is java.lang.OutOfMemoryError Java heap space 解决OOM
异常:Caused by: java.lang.IllegalStateException: Method has too many Body parameters Caused by: java.lang.IllegalStateException: Method has too many Body parameters: public abstract ...
主要介绍了Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
NULL 博文链接:https://redarmychen.iteye.com/blog/463864
Caused by: android.system.ErrnoException: write failed: ENOSPC (No space lef
java.lang.NoSuchFieldError: Companion 问题的解决方案
Caused by: java.lang.NoClassDefFoundError: com.sun.org.apache.xml.internal.resolver.CatalogManager at com.sun.xml.ws.util.xml.XmlUtil.createEntityResolver(XmlUtil.java:266) at org.jvnet.jax_ws_...
java 9 中引入了模块的概念,默认情况下,Java SE中将不再包含java EE 的Jar包 而在 java 6/7 / 8 时关于这个API 都是捆绑在
<groupId>com.squareup.okio</groupId> <artifactId>okio <version>2.8.0</version>
主要介绍了启动Azkaban报错:java.lang.NoSuchMethodError: com.google.common.collect.ImmutableMap.toImmutableMap,需要的朋友可以参考下
java.lang.NumberFormatException For inputstring 4294967295处理方法
java.lang.NoClassDefFoundError: com/sun/activation/registries/LogSupport异常处理
一、问题描述 Android studio导入一个项目报一堆错误: ... Java.lang.ExceptionInInitializerError at com.werb.gankwithzhihu.ui.fragment.ZhihuFragment.createPresenter(ZhihuFragment.java:33)
Caused by: java.sql.SQLException: java.lang.ClassNotFoundException: org.apache.hive.jdbc.HiveDriver at com.trs.gateway.commons.hive.HiveFeature.getConnection(HiveFeature.java:57) at ...