title: Memory Leak in Java Servlet
body: <p>I'm attempting to use a Java Servlet, but I end up using all of the system memory and tomcat gets killed by the kernel with the following error:</p>
  
  <pre><code>2018-06-18T17:55:49.238505+00:00 myserver kernel: Out of
  memory: Kill process 26117 (java) score 247 or sacrifice child
  
  2018-06-18T17:55:49.238937+00:00 myserver kernel: Killed
  process 26117 (java) total-vm:6298824kB, anon-rss:710520kB, file-rss:0kB,
  shmem-rss:0kB
  
  2018-06-18T17:55:49.241307+00:00 myserver systemd:
  tomcat.service: main process exited, code=killed, status=9/KILL
  </code></pre>
  
  <p>Sometimes the files that I have to serve to end users are large (~200 MB). If I demand multiple of them, or sometimes only one I get the above crash.    </p>
  
  <pre><code>case "xml":
      response.addHeader("Content-Disposition","attachment;filename=\"igrfgridData.xml\"");
      response.setContentType("text/xml");
  
      String line = "";
      while ((line = cResponse.readLine()) != null) {
          if(!line.startsWith("comment") &amp;&amp; !line.isEmpty())
               printWriter.print(line.toLowerCase() +"\n");
          }
      cResponse.close();
      break;
  </code></pre>
  
  <p>cResponse is a buffered reader from another server.  I'm not sure why this should use very much memory at all.  The lines are only ever 10's of characters long.  </p>
  
  <p>Wrinkle: free -m gives:</p>
  
  <pre><code>               total        used        free      shared  buff/cache   available
  Mem:            925         416          73           3         435         323
  Swap:          4095         174        3921
  </code></pre>
  
  <p>Tomcat is running with these memory parameters:
  -Xmx4096M -Xms4096M -XX:MaxPermSize=1024M -XX:PermSize=1024M</p>
  
  <p>Am I running tomcat with too much memory allocated?</p>