来自: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 = …;
分享到:
相关推荐
在Java应用程序运行过程中,"java.lang.OutOfMemoryError: PermGen space"错误是常见的一个问题,尤其是在使用Tomcat这样的Java应用服务器时。这个错误表明应用程序在 PermGen 区域(Permanent Generation)耗尽了...
Caused by: java.net.UnknownHostException: openapi.alipay.com
标题中的问题“scrcpy投屏 AssertionError: java.lang.reflect.InvocationTargetException”是用户在尝试使用Scrcpy时遇到的一个常见错误。这个错误通常意味着在执行某个方法时,Java运行时环境遇到了未预期的情况。...
整个系统都没有错误,但是在发布运行后,控制台上却抛出了这样的异常: java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource
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编程中,`java.lang.ClassNotFoundException`是一个常见的运行时异常,它表示JVM尝试加载一个类时,找不到对应的.class文件。在这个特定的错误中,我们看到的是`org.apache.commons.dbcp.BasicDataSource`,这...
Java程序在运行过程中可能会遇到各种异常,其中"nested exception is java.lang.OutOfMemoryError: Java heap space"是一个常见的问题,通常发生在程序试图分配超过堆内存限制的空间时。这个错误表明Java虚拟机(JVM...
异常: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`,这表明在运行时,系统无法找到`org.objectweb.asm.Type`这个类。`org.objectweb.asm` 是一个用于...
在这个特定的场景中,异常堆栈跟踪显示了 `Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections.Transformer`,这表明系统无法找到 `org.apache.commons.collections.Transformer` 类。...
### 错误解析:Caused by: android.system.ErrnoException: write failed: ENOSPC (No space left 在Android开发过程中,可能会遇到“Caused by: android.system.ErrnoException: write failed: ENOSPC (No space ...
"java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver" 解决方案 [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket. 解决了jsp连接 sql server 2000的问题
java.lang.NoSuchFieldError: Companion 问题的解决方案
Caused by: java.lang.RuntimeException: javax.management.remote.JMXServerErrorException: java.lang.AssertionError: Registered more than one instance with the same objectName : com.bea:Name=wl04,Type=...
在Java编程中,`java.lang.NoSuchMethodError`异常是一个运行时异常,通常发生在程序试图执行一个不存在的方法时。这个异常的出现通常意味着编译时和运行时的类版本不匹配,或者是在编译期间存在而运行时不存在的...
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory 需要jar log4j-1.2.16.jar slf4j-api-1.6.1.jar slf4j-log4j12-1.6.1.jar
在Java 6、7和8中,Java平台的标准版(Java SE)包含了Java企业版(Java EE)的一些API,如JAXB(Java Architecture for XML Binding)。然而,从Java 9开始,为了实现模块化并提高系统的可维护性和安全性,Java EE...