Optimal Configuration of Java Heap Options (-Xmx) for Tomcat 6 on Ubuntu 9.04: Best Practices for Package Management


1 views

When working with the official Ubuntu 9.04 tomcat6 package, it's crucial to recognize that the service runs through jsvc (Java Service) rather than a direct Java process. The package maintains configuration files in several locations:

/etc/default/tomcat6   # Environment variables
/etc/init.d/tomcat6    # Init script
/usr/share/tomcat6/    # Tomcat installation

The most maintainable solution is to modify /etc/default/tomcat6, which is specifically designed for such customizations and survives package updates:

# Add this line to /etc/default/tomcat6
JAVA_OPTS="-Xmx512m -Xms256m -XX:MaxPermSize=128m"

Alternatively, for more Tomcat-specific options:

CATALINA_OPTS="-Xmx512m -Xms256m -Djava.awt.headless=true"

Using the defaults file provides several advantages:

  • Package-safe: Survives apt-get upgrade operations
  • Centralized: All environment configuration in one place
  • Service-aware: Properly picked up by both init script and jsvc

After making changes, verify they're applied correctly:

sudo service tomcat6 restart
ps aux | grep tomcat | grep -v grep

You should see your memory parameters in the Java command line. For a more detailed view:

jinfo -flags <tomcat_pid>

While these work, they're not recommended for package-managed installations:

# 1. Modifying catalina.sh directly
# Problem: Gets overwritten on package updates

# 2. Editing the init script
# Problem: Breaks package verification (dpkg -S)

# 3. Creating custom startup scripts
# Problem: Service management becomes inconsistent

If your settings aren't applying:

  1. Check file permissions on /etc/default/tomcat6
  2. Verify you're restarting the service (not just reloading)
  3. Ensure no duplicate settings in multiple locations

For production systems, consider adding monitoring:

# In /etc/default/tomcat6
CATALINA_OPTS="$CATALINA_OPTS -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/log/tomcat6"

When working with Tomcat 6 on Ubuntu 9.04 through the official package (tomcat6), it's crucial to recognize the architecture differences from a manual Tomcat installation. The Ubuntu package uses jsvc to run Tomcat as a daemon, which affects how JVM parameters should be configured.

Let's examine the three main approaches mentioned and their implications:

# Option 1: Modifying /etc/init.d/tomcat6
# Not recommended as package updates may overwrite this file
JAVA_OPTS="-Xmx512m -Xms256m"

# Option 2: Editing catalina.sh
# Risky for the same reason - package maintainer scripts may overwrite
JAVA_OPTS="$JAVA_OPTS -Xmx512m"

Create or modify /etc/default/tomcat6 (Ubuntu's standard location for service configuration):

# Set Java heap size and other JVM parameters
JAVA_OPTS="-Xmx1024m -Xms512m -XX:MaxPermSize=256m"
export JAVA_OPTS

For environments with multiple Java applications, consider setting these at the system level:

# In /etc/environment (affects all users)
CATALINA_OPTS="-Xmx1536m -XX:+UseConcMarkSweepGC"

After making changes, verify they're being applied:

sudo service tomcat6 restart
ps aux | grep tomcat

The /etc/default/tomcat6 approach is safest because:

  • Ubuntu's package management system preserves this file during updates
  • It's the designated location for service configuration overrides
  • Separates system configuration from application files

For production environments, consider these additional parameters:

JAVA_OPTS="-server -Xmx2048m -Xms2048m -XX:NewSize=512m \
-XX:MaxNewSize=512m -XX:PermSize=256m -XX:MaxPermSize=256m \
-XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC \
-XX:+CMSParallelRemarkEnabled -XX:+UseCMSInitiatingOccupancyOnly \
-XX:CMSInitiatingOccupancyFraction=70"