2015-04-27

Tomcat Memory

Modern Java supports 64-bit architectures. That means you can throw many gigs of memory at Tomcat.

This is especially useful in Vaadin apps as the state all lives in web sessions in the Tomcat server. So those sessions tend to eat up memory.

My research into this topic was long and frustrating. For one thing, much discussion involves "PermGen" which is now outmoded. Current updates to Java 8 have eliminated the PermGen, so memory management is now more straightforward.

Eventually I learned one simple way to allocate memory to Tomcat 8. The following steps work on with the Oracle release of Java 8 on Mac OS X Mountain Lion. The steps probably work on any Unix-oriented system such as BSD or Linux. Don’t know about Microsoft Windows. The "-X" flags are optional, not required by the Java spec; so other non-Oracle implementations of Java may not respect these flags.

  1. Locate your Tomcat folder, and the nested the "bin" folder.
  2. Create a text file named exactly "setenv.sh". 
  3. Add one line to the file:
    export JAVA_OPTS="-Xms6144m -Xmx6144m"
Those two numbers should be what ever number of megs you want. So 6,144 megs is 6 gigs. I don’t know if it is required, but I would use multiples of 1024. So 6 * 1024 = 6,144.

By setting the minimum (Xms) to be the same number as the maximum (Xmx), Tomcat will grab and hold that entire amount of memory. If you set the minimum to be lower, you will see the amount of memory used by Tomcat grow and shrink over time as garbage collection releases memory back to the operating system. I do not know the pros and cons of these two behaviors.

4 comments: