Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
./setenv.sh - example setenv.sh with defaults set for minimal time spent in garbage collection
#! /bin/sh
# ==================================================================
# ______ __ _____
# /_ __/___ ____ ___ _________ _/ /_ /__ /
# / / / __ \/ __ `__ \/ ___/ __ `/ __/ / /
# / / / /_/ / / / / / / /__/ /_/ / /_ / /
#/_/ \____/_/ /_/ /_/\___/\__,_/\__/ /_/
# Multi-instance Apache Tomcat installation with a focus
# on best-practices as defined by Apache, SpringSource, and MuleSoft
# and enterprise use with large-scale deployments.
# Credits:
# Google -> Couldn't survive without it
# Stackoverflow.com -> Community support
# SpringSource -> Specifically best-practices and seminars (Expert Series)
# Based On:
# http://www.springsource.com/files/uploads/tomcat/tomcatx-performance-tuning.pdf
# http://www.springsource.com/files/u1/PerformanceTuningApacheTomcat-Part2.pdf
# http://www.springsource.com/files/uploads/tomcat/tomcatx-large-scale-deployments.pdf
# Created By: Terrance A. Snyder
# URL: http://www.terranceasnyder.com, http://shutupandcode.net
# Best Practice Documentation:
# http://terranceasnyder.com/2011/05/tomcat-best-practices/
# Looking for the latest version?
# github @ https://github.com/terrancesnyder
# ==================================================================
# discourage address map swapping by setting Xms and Xmx to the same value
# http://confluence.atlassian.com/display/DOC/Garbage+Collector+Performance+Issues
export CATALINA_OPTS="$CATALINA_OPTS -Xms64m"
export CATALINA_OPTS="$CATALINA_OPTS -Xmx512m"
# Increase maximum perm size for web base applications to 4x the default amount
# http://wiki.apache.org/tomcat/FAQ/Memoryhttp://wiki.apache.org/tomcat/FAQ/Memory
export CATALINA_OPTS="$CATALINA_OPTS -XX:MaxPermSize=256m"
# Reset the default stack size for threads to a lower value (by 1/10th original)
# By default this can be anywhere between 512k -> 1024k depending on x32 or x64
# bit Java version.
# http://www.springsource.com/files/uploads/tomcat/tomcatx-large-scale-deployments.pdf
# http://www.oracle.com/technetwork/java/hotspotfaq-138619.html
export CATALINA_OPTS="$CATALINA_OPTS -Xss192k"
# Oracle Java as default, uses the serial garbage collector on the
# Full Tenured heap. The Young space is collected in parallel, but the
# Tenured is not. This means that at a time of load if a full collection
# event occurs, since the event is a 'stop-the-world' serial event then
# all application threads other than the garbage collector thread are
# taken off the CPU. This can have severe consequences if requests continue
# to accrue during these 'outage' periods. (specifically webservices, webapps)
# [Also enables adaptive sizing automatically]
export CATALINA_OPTS="$CATALINA_OPTS -XX:+UseParallelGC"
# This is interpreted as a hint to the garbage collector that pause times
# of <nnn> milliseconds or less are desired. The garbage collector will
# adjust the Java heap size and other garbage collection related parameters
# in an attempt to keep garbage collection pauses shorter than <nnn> milliseconds.
# http://java.sun.com/docs/hotspot/gc5.0/ergo5.html
export CATALINA_OPTS="$CATALINA_OPTS -XX:MaxGCPauseMillis=1500"
# A hint to the virtual machine that it.s desirable that not more than:
# 1 / (1 + GCTimeRation) of the application execution time be spent in
# the garbage collector.
# http://themindstorms.wordpress.com/2009/01/21/advanced-jvm-tuning-for-low-pause/
export CATALINA_OPTS="$CATALINA_OPTS -XX:GCTimeRatio=9"
# The hotspot server JVM has specific code-path optimizations
# which yield an approximate 10% gain over the client version.
export CATALINA_OPTS="$CATALINA_OPTS -server"
# Disable remote (distributed) garbage collection by Java clients
# and remove ability for applications to call explicit GC collection
export CATALINA_OPTS="$CATALINA_OPTS -XX:+DisableExplicitGC"
# Check for application specific parameters at startup
if [ -r "$CATALINA_BASE/bin/appenv.sh" ]; then
. "$CATALINA_BASE/bin/appenv.sh"
fi
echo "Using CATALINA_OPTS:"
for arg in $CATALINA_OPTS
do
echo ">> " $arg
done
echo ""
echo "Using JAVA_OPTS:"
for arg in $JAVA_OPTS
do
echo ">> " $arg
done
echo "_______________________________________________"
echo ""
@poolebu

This comment has been minimized.

Show comment Hide comment
@poolebu

poolebu May 22, 2013

I am so grateful for this, it completely changed my production server's performance, and opened up a lot of useful knowledge. Thank you

poolebu commented May 22, 2013

I am so grateful for this, it completely changed my production server's performance, and opened up a lot of useful knowledge. Thank you

@zmirc

This comment has been minimized.

Show comment Hide comment
@zmirc

zmirc Jul 22, 2013

Thank you so much!

zmirc commented Jul 22, 2013

Thank you so much!

@thejoecarroll

This comment has been minimized.

Show comment Hide comment
@thejoecarroll

thejoecarroll Nov 21, 2013

This is just what I was looking for—thanks, @terrancesnyder ! One comment/question: you say you want to "discourage address map swapping by setting Xms and Xmx to the same value" but then use different vales in your script (-Xms64m & -Xmx512m), a couple of lines later; was this an error, or did you change your mind about the ideal values and forget to update the comment?

This is just what I was looking for—thanks, @terrancesnyder ! One comment/question: you say you want to "discourage address map swapping by setting Xms and Xmx to the same value" but then use different vales in your script (-Xms64m & -Xmx512m), a couple of lines later; was this an error, or did you change your mind about the ideal values and forget to update the comment?

@Avec112

This comment has been minimized.

Show comment Hide comment
@Avec112

Avec112 Mar 10, 2014

Thanks for sharing.

Avec112 commented Mar 10, 2014

Thanks for sharing.

@santoshd

This comment has been minimized.

Show comment Hide comment
@santoshd

santoshd Mar 12, 2014

With Java 7, I see a "Stack Size Too Small" and then tomcat refuses to startup. Recommended size is 228k.

With Java 7, I see a "Stack Size Too Small" and then tomcat refuses to startup. Recommended size is 228k.

@nmalservet

This comment has been minimized.

Show comment Hide comment
@nmalservet

nmalservet Jul 8, 2014

Thanks. Very useful.
I 've found the same message from tomcat "The stack size specified is too small, Specify at least 228k", so i need to increase the Xss by using this value " -Xss256k" and to prevent a permgen space i use a different value : "-XX:MaxPermSize=512m"

Thanks. Very useful.
I 've found the same message from tomcat "The stack size specified is too small, Specify at least 228k", so i need to increase the Xss by using this value " -Xss256k" and to prevent a permgen space i use a different value : "-XX:MaxPermSize=512m"

@scduvv2

This comment has been minimized.

Show comment Hide comment
@scduvv2

scduvv2 Dec 14, 2014

Thanks a lot

scduvv2 commented Dec 14, 2014

Thanks a lot

@tonyvu2014

This comment has been minimized.

Show comment Hide comment
@tonyvu2014

tonyvu2014 Mar 26, 2015

This is very cool. It increases my tomcat performance significantly. Thanks.

This is very cool. It increases my tomcat performance significantly. Thanks.

@sirkazey

This comment has been minimized.

Show comment Hide comment
@sirkazey

sirkazey Mar 31, 2015

Thanks a lot!

Thanks a lot!

@jouna

This comment has been minimized.

Show comment Hide comment
@jouna

jouna Jun 3, 2015

Thanks for sharing!

jouna commented Jun 3, 2015

Thanks for sharing!

@wenzhucjy

This comment has been minimized.

Show comment Hide comment
@wenzhucjy

wenzhucjy Oct 12, 2015

Thanks

Thanks

@gusthavosouza

This comment has been minimized.

Show comment Hide comment
@gusthavosouza

gusthavosouza Nov 18, 2015

Thanks.

Thanks.

@warehemant

This comment has been minimized.

Show comment Hide comment
@warehemant

warehemant Dec 9, 2015

Thanks .. Really very helpful..

Thanks .. Really very helpful..

@nickk75

This comment has been minimized.

Show comment Hide comment
@nickk75

nickk75 May 18, 2016

Thanks for the knowledge sharing dude, much appreciated

nickk75 commented May 18, 2016

Thanks for the knowledge sharing dude, much appreciated

@wimco

This comment has been minimized.

Show comment Hide comment
@wimco

wimco Jun 21, 2016

Hi Guys i have installed Alfresco community 5.0.e on Ubuntu 14.04 LTS and i cant find setenv.sh file. What can I do i want to run javascript debugger

wimco commented Jun 21, 2016

Hi Guys i have installed Alfresco community 5.0.e on Ubuntu 14.04 LTS and i cant find setenv.sh file. What can I do i want to run javascript debugger

@davidoram

This comment has been minimized.

Show comment Hide comment
@davidoram

davidoram Jun 25, 2017

Thanks!

Thanks!

@MathiasZaja

This comment has been minimized.

Show comment Hide comment
@MathiasZaja

MathiasZaja Oct 7, 2017

The various CATALINA_OPTS such as -Xms , Xmx, XX:MaxPermSize seem to be independant of physical RAM.
Is it right ?
What are the default values (for Tomcat 7) ?

The various CATALINA_OPTS such as -Xms , Xmx, XX:MaxPermSize seem to be independant of physical RAM.
Is it right ?
What are the default values (for Tomcat 7) ?

@MathiasZaja

This comment has been minimized.

Show comment Hide comment
@MathiasZaja

MathiasZaja Oct 7, 2017

It does not work for me
Creating setenv.sh with just export CATALINA_OPTS="$CATALINA_OPTS -Xmx512m" in it crashes tomcat7 on start.
$CATALINA_OPTS is not defined in my standard tomcat7 installation on Debian Jessie.
Should I define it somewhere ?
Regards

It does not work for me
Creating setenv.sh with just export CATALINA_OPTS="$CATALINA_OPTS -Xmx512m" in it crashes tomcat7 on start.
$CATALINA_OPTS is not defined in my standard tomcat7 installation on Debian Jessie.
Should I define it somewhere ?
Regards

@mahamuniraviraj

This comment has been minimized.

Show comment Hide comment
@mahamuniraviraj

mahamuniraviraj Mar 28, 2018

thank you so much

thank you so much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment