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:
- Is the whole memory used by Tomcat should be 256MB or Tomcat consumes other memory for other reasons ?
- 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.
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:
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.)
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.
Tomcat needs to increase permgen not for its own needs but because it can run multiple webapps which all will load their own classes (permgen is mostly for storing classes). You can use jconsole util too see memory usage, go to memory -> chart -> Memory Pool Perm Gen. For my Tomcat 7 which I just started with no user webapps it shows 65M max 6M used.