# How to Increase Tomcat Default Memory Size

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Sep 21, 2019. Topics: Java, DevOps.
Canonical URL: https://attacomsian.com/blog/tomcat-increase-default-memory-size

> Find out how to increase the default heap memory size for Apache Tomcat web server.

[Apache Tomcat](https://tomcat.apache.org) 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*`.

<p class="file-desc"><span>setenv.sh</span></p>

```bash
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` &mdash; 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` &mdash; The maximum Java heap size in megabytes. By default, the maximum heap size value is 256 MB.
* `-XX:PermSize` &mdash; 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` &mdash; The maximum permanent generation size.

## Windows Environment

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

<p class="file-desc"><span>setenv.bat</span></p>

```bash
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.
