We have already discussed about how to install Tomcat in Linux system, this post will explore set or increase the Java heap size for the application.

By default Tomcat server sets its own memory heap size 64MB but its not enough for run your application, so we can set size  by CATALINA_OPTS or JAVA_OPTS,

-Xmx   set Maximum Java Heap Size
-Xms   set Initial Java Heap Size

Option 1:

Open the catalina.sh script file under $CATALINA_HOME/bin/catalina.sh directory assign it,

export CATALINA_OPTS="-Xms128M -Xmx512M"

or simply export,

export CATALINA_OPTS="-Xms128M -Xmx512MB"

The heap size has been set to Minumum 128MB and maximum 512MB

Option 2:

$CATALINA_HOME/bin/setenv.sh


Another option is create a setenv.sh file and add below line,

export CATALINA_OPTS="${CATALINA_OPTS} -Djava.awt.headless=true -Dorg.apache.jasper.runtime.BodyContentImpl.LIMIT_BUFFER=true -Dmail.mime.decodeparameters=true -Xms128m -Xmx512m -XX:PermSize=256m -XX:MaxPermSize=950m -XX:+CMSClassUnloadingEnabled -XX:+CMSPermGenSweepingEnabled"

or
export CATALINA_OPTS="-Xms128M -Xmx512MB"


You might restart tomcat server,
 
# pkill -9 java

# /usr/local/apache-tomcat/bin/startup.sh

# ps aux | grep java

root      9447  1.1 13.2 2127844 543868 ?      Sl   17:20   3:38 /usr/java/jdk1.7.0/bin/java -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.util.logging.config.file=/usr/local/tomcat7/conf/logging.properties -Djava.awt.headless=true -Dorg.apache.jasper.runtime.BodyContentImpl.LIMIT_BUFFER=true -Dmail.mime.decodeparameters=true -Xms128m -Xmx512m -XX:MaxPermSize=512m -XX:+CMSClassUnloadingEnabled -XX:+CMSPermGenSweepingEnabled -Djava.endorsed.dirs=/usr/local/tomcat7/endorsed -classpath :/usr/local/tomcat6/bin/bootstrap.jar:/usr/local/tomcat7/bin/commons-logging-api.jar -Dcatalina.base=/usr/local/tomcat7 -Dcatalina.home=/usr/local/tomcat7 -Djava.io.tmpdir=/usr/local/tomcat6/temp org.apache.catalina.startup.Bootstrap start
root     12761  0.0  0.0 103244   868 pts/0    S+   22:24   0:00 grep java

or can use netstat command,
 
# netstat -anp | grep java

tcp        0      0 :::8009                     :::*                        LISTEN      9447/java           
tcp        0      0 :::8080                     :::*                        LISTEN      9447/java           
tcp        0      0 ::ffff:127.0.0.1:8005       :::*                        LISTEN      9447/java           
tcp        0      0 ::ffff:127.0.0.1:35334      ::ffff:127.0.0.1:3306       ESTABLISHED 9447/java           
tcp        0      0 ::ffff:127.0.0.1:56156      ::ffff:127.0.0.1:3306       ESTABLISHED 9447/java           
unix  2      [ ]         STREAM     CONNECTED     113825 9447/java 


Windows  Server:

To set in windows operating system, Create a setenv.bat file and add the below line,
export JAVA_OPTS="-server -Xmx512m"

or
set JAVA_OPTS="-Xms256m -Xmx512m"


Error:

If you may get an error java.lang.OutOfMemoryError: PermGen space in both windows or linux system,

Configure the MaxPermSize value in  setenv.sh or catalina.sh like,

export JAVA_OPTS="-Xms256m -Xmx512m -XX:MaxPermSize=128m"

If you have changed any configuration might restart tomcat server.