title: jetty continuation and unexplainable memory usage pattern
body: <p>I have been struggling with something that looks very basic, the problem is related to use of Jetty continuations for long poll.</p>
  
  <p>For the sake of simplicity, i have removed all my application specific code and just left simple continuation related code.</p>
  
  <p>I am pasting the doPost method of my servlet below. The key question, where i need some expert guidance is</p>
  
  <ul>
  <li>In the code block below, if i run it as is and fire post requests which carry a post body of approx 200 bytes then the amount of memory for 500 long poll connections is around 20 MB.</li>
  <li>Where as if I comment the block highlighted as "decrease memory footprint :: comment block below" then the memory foot print comes down to 7 MB</li>
  </ul>
  
  <p>In both the cases i wait for system to be stable, call GC multiple times and then take memory reading via jConsole. Its not exact, but the difference is so much and explanable that precision of few 100 bytes here or there does not matter.</p>
  
  <p>My problem explodes, considering my server is required to hold 100K connections if not more. And here and this unexplanable increase in size eventually leads to close to GBs of extra heap used.</p>
  
  <p>( what is causing this extra heap usage, when even what is read from the stream is not preserved outside the scope of doPost method. But still it adds to the heap....what am i missing?)</p>
  
  <pre><code>   @Override
     protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
  
      Continuation cc = ContinuationSupport.getContinuation(req);
  
      //if continuation is resumed, then send an answer back with 
      //hardcoded answer
      if (cc.isResumed()) {
          String myJson = "{\"1\",\"2\"}";
          res.setContentType("application/json");
          res.setContentLength(myJson.length());
          PrintWriter writer = res.getWriter();
          writer.write(myJson);
          writer.close();
      } 
      // if it is the first call to doPost ( not reentrant call )
      else if (cc.isInitial()) {          
  
          //START :: decrease memory footprint :: comment this block :: START
  
          // store the json from the request body in a string
          StringBuffer jsonString = new StringBuffer();
          String line = null;                      
          BufferedReader bufferedReader = req.getReader();
          while ((line = bufferedReader.readLine()) != null) {
              jsonString.append(line);
          }  
  
          //here jsonString was parsed and some values extracted
          //though that code is removed for the sake of this publish
          // as problem exists irrespective...of any processing
  
          line = null;            
          bufferedReader.close();
          bufferedReader = null;
          jsonString = null;
  
          // END :: decrease memory footprint :: comment this block :: END
  
          cc.setTimeout(150000);        
  
          cc.suspend();
      }
  }
  </code></pre>