title: Tomcat Fix Memory Leak?
body: <p>I am using 6.0.20 I have a number of web apps running on the server, over time, approximately 3 days and the server needs restarting otherwise the server crashes and becomes unresponsive.</p>
  
  <p>I have the following settings for the JVM:</p>
  
  <pre><code>-XX:+HeapDumpOnOutOfMemoryError
  -XX:HeapDumpPath=c:\tomcat\Websites\private\mydomain\apache-tomcat-6.0.20\logs
  </code></pre>
  
  <p>This provides me with a hprof file which I have loaded using Java VisualVM which identifies the following:</p>
  
  <pre><code>byte[] 37,206   Instances | Size 86,508,978
  int[] 540,909   Instances | Size 55,130,332
  char[] 357,847  Instances | Size 41,690,928
  </code></pre>
  
  <p>The list goes on, but how do I determine what is causing these issues?</p>
  
  <p>I am using New Relic to monitor the JVM and only one error seems to appear but it's a reoccurring one, org.apache.catalina.connector. ClientAbortException. Is it possible that when a user session is aborted, any database connections or variables created are not being closed and are therefore left orphaned?</p>
  
  <p>There is a function which is used quite heavily throughout each web app, not sure if this has any bearing on the leak:</p>
  
  <pre><code>public static String replaceCharacters(String s)
  {
      s = s.replaceAll("  ", " ");
      s = s.replaceAll(" ", "_");
      s = s.replaceAll("\351", "e");
      s = s.replaceAll("/", "");
      s = s.replaceAll("--", "-");
      s = s.replaceAll("&amp;", "and");
      s = s.replaceAll("&amp;amp;", "and");
      s = s.replaceAll("__", "_");
      s = s.replaceAll("\\(", "");
      s = s.replaceAll("\\)", "");
      s = s.replaceAll(",", "");
      s = s.replaceAll(":", "");
      s = s.replaceAll("\374", "u");
      s = s.replaceAll("-", "_");
      s = s.replaceAll("\\+", "and");
      s = s.replaceAll("\"", "");
      s = s.replaceAll("\\[", "");
      s = s.replaceAll("\\]", "");
      s = s.replaceAll("\\*", "");
      return s;
  }
  </code></pre>
  
  <p>Is it possible that when a user connection is aborted, such as a user browser closed or the users has left the site that all variables, connections, etc... are purged/released, but isn't GC supposed to handled that?</p>
  
  <p>Below are my JVM settings:</p>
  
  <pre><code>-Dcatalina.base=c:\tomcat\Websites\private\mydomain\apache-tomcat-6.0.20
  -Dcatalina.home=c:\tomcat\Websites\private\mydomain\apache-tomcat-6.0.20
  -Djava.endorsed.dirs=c:\tomcat\Websites\private\mydomain\apache-tomcat-6.0.20\endorsed
  -Djava.io.tmpdir=c:\tomcat\Websites\private\mydomain\apache-tomcat-6.0.20\temp
  -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
  -Djava.util.logging.config.file=c:\tomcat\Websites\private\mydomain\apache-tomcat-6.0.20\conf\logging.properties
  -Dfile.encoding=UTF-8
  -Dsun.jnu.encoding=UTF-8
  -javaagent:c:\tomcat\Websites\private\mydomain\apache-tomcat-6.0.20\newrelic\newrelic.jar
  -XX:+HeapDumpOnOutOfMemoryError
  -XX:HeapDumpPath=c:\tomcat\Websites\private\mydomain\apache-tomcat-6.0.20\logs
  -Dcom.sun.management.jmxremote.port=8086
  -Dcom.sun.management.jmxremote.ssl=false
  -Dcom.sun.management.jmxremote.authenticate=false vfprintf
  -Xms1024m
  -Xmx1536m
  </code></pre>
  
  <p>Am I missing anything? The server has 3GB ram.</p>
  
  <p>Any help would be much appreciated :-)</p>