Tomcat 7 - cannot get CATALINA_OPTS heap size to work

arahant picture arahant · Apr 1, 2013 · Viewed 8.5k times · Source

I am setting CATALINA_OPTS in catalina.sh but strangely this value seems to be ignored. I am on a machine with 768M RAM. Here are the steps to reproduce

I check the memory before starting Tomcat by running free -m. I get the following

    total       used       free     shared    buffers     cached
    768         43        724          0          0         16

I have set the CATALINA_OPTS in the first line of catalina.sh like so:

export CATALINA_OPTS="-Xms64M -Xmx256M"

I then start Tomcat using ./startup.sh

I check that Tomcat took the arguments by doing a ps aux | grep tomcat . I get the following result which seems to indicate that the setting was read

root 1164 26.0 6.2 1232968 49116 pts/0 Sl 10:30 0:01 /usr/bin/java -Djava.util.logging.config.file=/usr/share/apache-tomcat-7.0.39/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Xms64M -Xmx256M -Djava.endorsed.dirs=/usr/share/apache-tomcat-7.0.39/endorsed -classpath /usr/share/apache-tomcat-7.0.39/bin/bootstrap.jar:/usr/share/apache-tomcat-7.0.39/bin/tomcat-juli.jar -Dcatalina.base=/usr/share/apache-tomcat-7.0.39 -Dcatalina.home=/usr/share/apache-tomcat-7.0.39 -Djava.io.tmpdir=/usr/share/apache-tomcat-7.0.39/temp org.apache.catalina.startup.Bootstrap start

I again run free -m but I get

       total       used       free     shared    buffers     cached
       768        558        209          0          0         20

This suggests that 512MB was used. I am not sure what I am doing wrong. I also tried setting JAVA_OPTS but that did not work either.

I am on CentOS 6 with JDK7 and Apache Tomcat 7.

EDIT 1: Here are the results if I set the -Xmx values to 128 and 64 respectively

       total       used       free     shared    buffers     cached
       768        432        335          0          0         39

and

       total       used       free     shared    buffers     cached
       768        366        401          0          0         32

Looks like the -Xms setting has no effect at all and approx 250MB over and above the value of the -Xmx setting is being used.

This is rather hard to explain. It is a plain vanilla Tomcat 7 installation with no webapps installed (other than the default ones). Surely they don't take 250 MB, do they?

Answer

Olaf Kock picture Olaf Kock · Apr 1, 2013

Note that the heap is not the only block of memory that java acquires. Also, if tomcat's startup process uses up more than 64M, the JVM will happily allocate more memory up to the -Xmx value. But that's not everything - the stacks for the various stacks take memory, as well as the classes to load. There's the PermGen memory that's taken on top of the heap memory as well.

Check this article for more discussion on the various memory you're dealing with. Given today's prices for memory, it might just be cheaper to upgrade the server to have more memory than worrying about optimizing the memory consumption of the application on this level.