why do i have a defaultChannelFuture memory leak?

vasion momchilrogelov at gmail.com
Mon Jul 19 05:29:33 EDT 2010


i am having a hard time figuring out why my memory leaks. the profiler in
netbeans shows defaultChannelFuture to be the culprit. How can i fix this?
it is a very simple little server app. I am including code and screenshot of
profiler 

A php script connects to the server and spits out xml followed by 0
byte("chr(0)"). the server then send it to everybody else connected.

http://netty-forums-and-mailing-lists.685743.n2.nabble.com/file/n5311265/netty.png 


server:

import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;



public class RadioPopuliServer {
      public static void main(String[] args) throws Exception {
	// Configure the server.
    	  ServerBootstrap bootstrap = new ServerBootstrap(
              	new NioServerSocketChannelFactory(
                      	Executors.newCachedThreadPool(),
                      	Executors.newCachedThreadPool()));
 // Set up the default event pipeline.

         
        RadioPopuliHandler handler = new RadioPopuliHandler();



        bootstrap.getPipeline().addLast("handler", handler);
// Bind and start to accept incoming connections.

        bootstrap.bind(new InetSocketAddress(8080));
    }
}

the handler:

import java.util.logging.Level;
import java.util.logging.Logger;
import java.net.InetSocketAddress;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipelineCoverage;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
import org.jboss.netty.channel.group.ChannelGroup;
import org.jboss.netty.channel.group.DefaultChannelGroup;
/**

*handler for RadioPopuliServer

 *
 **/

@ChannelPipelineCoverage("all")

public class RadioPopuliHandler extends SimpleChannelUpstreamHandler {

	private static final Logger logger =
Logger.getLogger(RadioPopuliHandler.class.getName());
	public ChannelGroup allchannels = new DefaultChannelGroup();
        private InetSocketAddress address;

@Override
	public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e)
throws Exception {
                    allchannels.add(e.getChannel());
			}


@Override
    public void messageReceived(
            ChannelHandlerContext ctx, MessageEvent e) {
        // Send back the received message to the remote peer
                //
                Runtime r = Runtime.getRuntime();
                long freeMem = r.freeMemory();
                System.out.println("free memory :  " + freeMem);

                address = (InetSocketAddress)(e.getRemoteAddress());
                if(address.getAddress().isLoopbackAddress()){
                    allchannels.write(e.getMessage());
                    
                }
                address = null;

                

      // Step 5: run the garbage collector, then check freeMemory
              r.gc();
              




    }



@Override
    public void exceptionCaught(
            ChannelHandlerContext ctx, ExceptionEvent e) {
        // Close the connection when an exception is raised.
        logger.log(
                Level.WARNING,
                "Unexpected exception from downstream.",
                e.getCause());
        e.getChannel().close();

    }

@Override
    public void channelDisconnected(ChannelHandlerContext ctx,
ChannelStateEvent e){
        allchannels.remove(e.getChannel());
        System.out.println("Channel closed");
    }

}

-- 
View this message in context: http://netty-forums-and-mailing-lists.685743.n2.nabble.com/why-do-i-have-a-defaultChannelFuture-memory-leak-tp5311265p5311265.html
Sent from the Netty User Group mailing list archive at Nabble.com.


More information about the netty-users mailing list