In this post we will see a way of setting Apache Tomcat, version 7, environment variables when running under Debian 6.
Let’s suposse that Apache Tomcat – Tomcat for short – has been installed in the directory TOMCAT_DIR and that we want to set the used Java virtual machine and the amount of used RAM memory.
To accomplish it, in the directory $TOMCAT_DIR/bin a file named setenv.sh is created. The contents of this file are shown below [note 1]:
#!/bin/sh export CATALINA_OPTS="-Xms512m -Xmx512m" export JAVA_HOME="/usr/local/sun-java-6/jre1.6.0_31"
This approach has two advantages:
- Portability: Since settings are kept stored in a file, it that can be copied to other Tomcat installations or reused when upgrading Tomcat.
- Isolation: By using the environment variable CATALINA_OPTS the memory settings only apply to Tomcat, not all the Java applications running on the computer.
[note 1] A frequent advice about memory configuration in Tomcat says that the parameter Xms (inital heap size) should be set to the same value than Xmx (maximun heap size). Kees Jan Koster has written an interesting discussion about this issue at java-monitor.com: “Should we set -Xms to match -Xmx?“. Another interesting discussion about these and related settings is “Understanding Java’s “Perm Gen” (MaxPermSize, heap space, etc.)“