使用Netty在24分钟内泄漏9 kb内存

时间:2012-11-23 05:05:14

标签: java memory-leaks netty

9184字节内存在以下代码中泄漏

主要类别:

import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.jboss.netty.handler.timeout.IdleStateHandler;
import org.jboss.netty.util.HashedWheelTimer;
import org.jboss.netty.util.Timer;


    public class NettyMemoryLeakTest {

        public static void main(String[] args) {
            final Timer timer = new HashedWheelTimer();
            final IdleStateHandler idle = new IdleStateHandler(timer, 0, 0, 1);
            final ChannelFactory cf = new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool());
            final ServerBootstrap bootStrap = new ServerBootstrap(cf);
            final ServerHandler objServerHandler = new ServerHandler();
            bootStrap.setPipelineFactory(new ChannelPipelineFactory() {
                @Override
                public ChannelPipeline getPipeline() throws Exception {
                    ChannelPipeline pipeline = Channels.pipeline();
                    pipeline.addLast("IDLE", idle);
                    pipeline.addLast("Handler", objServerHandler);
                    return pipeline;

                }
            });

            bootStrap.bind(new InetSocketAddress(8080));

        }
    }

处理程序:

import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.handler.timeout.IdleState;
import org.jboss.netty.handler.timeout.IdleStateAwareChannelHandler;
import org.jboss.netty.handler.timeout.IdleStateEvent;

public class ServerHandler extends IdleStateAwareChannelHandler {

    @Override
    public void channelIdle(ChannelHandlerContext ctx, IdleStateEvent e) {
        if (e.getState() == IdleState.ALL_IDLE) {

            System.gc();

        }
    }
}

使用jdk 1.7,netty 3.5.8 Final 我通过netbeans profiler检查内存泄漏。它显示了内存的变化。使用的内存

48分钟后的泄漏量 2020kb 请帮我找出问题

谢谢。

1 个答案:

答案 0 :(得分:0)

System.gc()将不保证完全使用GC。如果您真的认为它是内存泄漏,请以二进制形式进行堆转储并提供检查。