0

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 分析器检查内存泄漏。它显示了内存的变化。使用的内存

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

谢谢你。

4

1 回答 1

0

System.gc() 根本不保证会进行 GC。如果您真的认为这是内存泄漏,请以二进制形式进行堆转储并将其提供给检查。

于 2012-11-23T05:50:09.923 回答