Search code examples
javatomcatmemory-managementout-of-memorypermgen

Tomcat memory usage (PermGen)


I am using Tomcat for deploying a web application withe following PermGen configurations:

JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8 
-server -Xms1536m -Xmx1536m
-XX:NewSize=256m -XX:MaxNewSize=256m -XX:PermSize=256m 
-XX:MaxPermSize=256m -XX:+DisableExplicitGC"

Please I want to know the following:

  1. Is the whole memory used by Tomcat should be 256MB or Tomcat consumes other memory for other reasons ?
  2. How can I monitor the memory used by Tomcat to avoid reaching PemGen max size ?

Also please be noticed that I have 3 Tomcats with the same previous PermGen configurations, running on the same physical server with 8 GB Ram which make me afraid of increasing PermGen max size to 512MB not to affect the physical server memory.


Solution

  • Permgen OOMEs in Tomcat are typically due a combination of two things:

    • Hot redeployment.

    • Memory leaks where some reference causes an old classloader and all of its dependent objects to remain reachable.

    Consequently, there are a couple of ways to avoid this problem:

    • Don't do hot redeploys. Or restart every few hot redeploys.

    • Track down and fix the classloader-related storage leaks.

    Increasing the permgen size won't prevent the problem, but it is likely to increase the interval between OOME problems. (Or allow you to do a full Tomcat shutdown / restart less often.)


    Your question:

    1) Is the whole memory used by Tomcat should be 256MB or Tomcat consumes other memory for other reasons ?

    See above. It is not specifically Tomcat, though Tomcat is particularly prone to this. (Apparently, Hibernate can be too ... due, I believe, to its use of dynamic proxies.)

    2) How can I monitor the memory used by Tomcat to avoid reaching PemGen max size ?

    There are ways to monitor memory usage; e.g. attaching an agent. However, these won't prevent the problem. IMO, it would be simpler to change your management procedures for your production server(s) so that you don't need to do hot redeploys.