Netty内存泄露
来源:互联网 发布:影楼照片后期制作软件 编辑:程序博客网 时间:2024/05/07 18:51
在测试中发现,当不停的开关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();
}
抛出的异常信息:
询问了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规避,以减少泄露:
测试代码:
- 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();
- }
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.<init>(DirectByteBuffer.java:95)
- at java.nio.ByteBuffer.allocateDirect(ByteBuffer.java:288)
- at org.jboss.netty.channel.socket.nio.SocketSendBufferPool$Preallocation.<init>(SocketSendBufferPool.java:159)
- at org.jboss.netty.channel.socket.nio.SocketSendBufferPool.<init>(SocketSendBufferPool.java:46)
- at org.jboss.netty.channel.socket.nio.NioWorker.<init>(NioWorker.java:84)
- at org.jboss.netty.channel.socket.nio.NioClientSocketPipelineSink.<init>(NioClientSocketPipelineSink.java:74)
- at org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory.<init>(NioClientSocketChannelFactory.java:135)
询问了Netty的作者Trustin Lee:
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 0
- Netty内存泄露
- Netty内存泄露
- netty内存泄露检测原理
- Netty内存泄露检测原理分析
- 一次关于Netty+Gson造成内存泄露的分析排查
- 一次关于Netty+Gson造成内存泄露 Memory Analysis分析
- **[netty内存管理]netty
- 内存泄露
- 内存泄露
- 内存泄露
- 内存泄露
- 内存泄露
- 内存泄露
- 内存泄露
- 内存泄露
- 内存泄露
- 内存泄露
- 内存泄露
- C语言中volatile关键字的作用
- scanf()返回值
- android如何使用自定义JNI接口,以及NDK工具的环境搭建与使用。
- 虚拟化(四):vsphere高可用功能前提-共享存储搭建
- app打包成ipa
- Netty内存泄露
- StringUtils的isBlank与isEmply
- Cocos2d-x3.0如何绑定调用lua回调函数
- 多柱汉诺塔最优算法设计探究
- JAVA webservice之xfire
- SSH框架搭建(附带,框架写的标准的三层架构小程序,实现简单的登陆校验,增删改查功能)
- 简单的大正整数计算器
- 无限级分类数据表设计的简单总结
- Struts的上传