Apache Tomcat is an open-source web server with a servlet container for running web Java applications. Developed and maintained by Apache Software Foundation, Tomcat is a very popular web container that is used by top companies in the world.

By default, the memory allocated to Tomcat is good enough for running a small application. As the application grows, the memory usage also increases. If you continue with the default Tomcat configuration, you might see frequent OutOfMemoryError exceptions due to low memory space. So it is very critical to allocate sufficient memory while running the application in a production environment.

To configure an appropriate heap memory and perm gen size, you have to create a file named setenv.sh in a Unix environment or setenv.bat in Windows. By default, this file is not included in Tomcat installation packages. You need to manually create for customizing default configuration

Unix Environment

For Unix machines, you should create a new script file called setenv.sh under $CATALINA_HOME/bin directory with the following contents:

Note: If you installed the Tomcat through apt package manager on your Unix machine, the default $CATALINA_HOME location is /usr/share/tomcat*.

setenv.sh

export JAVA_OPTS="-Dfile.encoding=UTF-8 -Xms256m -Xmx2048m -XX:PermSize=256m -XX:MaxPermSize=1024m"

Save the file and then restart the Tomcat server for the changes to take effect. That's it. You are done with increasing the default heap memory and perm gen size.

Here is a brief introduction to the above terms:

  • -Xms — This is the initial and minimum Java heap size in megabytes. By default, there is no value specified for this field. The heap is basically the memory space which holds all the objects created by your application.
  • -Xmx — The maximum Java heap size in megabytes. By default, the maximum heap size value is 256 MB.
  • -XX:PermSize — This is the initial size for permanent generation (or perm gen). It is the place where Tomcat caches classes and other resources in the memory.
  • -XX:MaxPermSize — The maximum permanent generation size.

Windows Environment

Create a new script file called setenv.bat under $CATALINA_HOME/bin directory with the following contents:

setenv.bat

export JAVA_OPTS="-Dfile.encoding=UTF-8 -Xms256m -Xmx2048m -XX:PermSize=256m -XX:MaxPermSize=1024m"

Once the file is created successfully, restart the Tomcat server for changes to take effect. That was all you need to do.

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.