Configure tomcat memory usage
By default, the memory size allocated for tomcat is not enough to handle heavy applications which require high memory usage, several OutOfMemoryError exceptions will be thrown if you keep the default tomcat configuration. In order to set the appropriate heap size and perm gen size, tomcat requires a file named as setenv.sh in unix environments and setenv.bat in windows. This file is not included in the tomcat package or installation, and it is manually created under the bin directory in case the administrator needs to tune the default tomcat memory configuration.
1. setenv script
Following is the process for creating setenv script in both windows and unix machines:
Create a new script named as setenv.bat under TOMCAT_HOME/bin folder holding the following content:
set JAVA_OPTS=-Dfile.encoding=UTF-8 -Xms128m -Xmx2048m -XX:PermSize=265m -XX:MaxPermSize=1024m
P.S: If tomcat is installed as a windows service, you can also set the memory configuration through running Tomcat8w.exe under bin folder as the following:
- Run Tomcat8w.exe under bin folder.
- Set the arguments inside Java tab.
Create a new script named as setenv.sh under TOMCAT_HOME/bin directory holding the following content:
export JAVA_OPTS="-Dfile.encoding=UTF-8 -Xms128m -Xmx2048m -XX:PermSize=265m -XX:MaxPermSize=1024m"
After creating the script, tomcat needs to be restarted for the changes to take effect.
2. setenv arguments
-Xms: This the initial java heap size.
-Xmx: This is the maximum java heap size.
The heap is the memory space which holds all the objects created by your application, it is the memory space allocated for your application, normally an application wouldn’t require more than maximum 2 GB of memory. In case of low heap space, “OutOfMemoryError: java heap space” exception is thrown.
-XX:PermSize: This is the initial perm gen size.
-XX: MaxPermSize: This is the maximum perm gen size.
The perm gen size is the space where your code base is stored inside the memory , the bigger your code base is, the more perm gen space is required, normally application wouldn’t require more than maximum 1 GB of Perm gen space. In case of low perm gen space, “OutOfMemoryError: PermGen” exception is thrown.