users@grizzly.java.net

Comet - weird memory behaviour

From: FredrikJ <fredrik_at_robotsociety.com>
Date: Mon, 27 Apr 2009 07:30:28 -0700 (PDT)

I have been implementing a Comet based solution recently and I noticed a
memory pattern that looked like a memory leak. After some trial and error I
ended up with a very simple servlet:


public class SimpleServlet extends HttpServlet {
        
        @Override
        public void init(ServletConfig config) throws ServletException {}

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
                try {
                        Thread.sleep(200);
                } catch (InterruptedException e) {}
                
                PrintWriter writer = resp.getWriter();
                writer.write("World's Simplest OOM Servlet");
                writer.flush();
        }
        
}


The memory-pattern not entirely deterministic, but the basic behavior is
like this:

- If I run requests against the servlet with a default connector (i.e. I
have not specified cometSupport in domain.xml), I get no surprises. The
memory churn is very low and all memory is recovered after the responses
have been sent.

- If I add <property name="cometSupport" value="true"/> to the domain.xml
for the http connector, then memory goes up drastically and then stays up.
For 500 requests the old gen memory goes up from about 50MB to 130MB. The
memory is never recovered, I have waited for timeouts and repeatedly forced
GC's.

Note: The memory issue only occurs when I have a substantial delay
(Thread.sleep) in the servlet. If I return the response immediately then I
do not see this behavior.

My first reactions was that it was an obvious memory leak coupled to request
handling with a certain timeframe, but it also seems that this only occurs
for the first batch I run against the server. For instance, if I apply 500
requests from JMeter then I will have roughly 130MB sitting around in old
gen. But any consecutive 500 requests does not further increase the static
old gen allocation.

A heap dump reveals that after 500 requests I usually have about 450
requests, responses and sessions in the heap. If I run 1000 requests
initially I end up with about 600. These seem to never be garbage collected.

Any clarifications on why I am seeing this memory pattern would be very much
appreciated since I am currently very reluctant on putting Comet into
production with this type of behavior.

Note 2: I am currently using Glassfish 2.1.



-- 
View this message in context: http://www.nabble.com/Comet---weird-memory-behaviour-tp23257612p23257612.html
Sent from the Grizzly - Users mailing list archive at Nabble.com.