[java] How to increase Java heap space for a tomcat app

There are lots of questions that ask this or a similar question.

They all give the command that has to be executed, what I don't understand is where do I write this command. I want to permanently increase the heap space for my tomcat apps.

I read this page http://javahowto.blogspot.com/2006/06/6-common-errors-in-setting-java-heap.html and it says under the Tomcat section

Stop Tomcat server, set environment variable CATALINA_OPTS, and then restart Tomcat. Look at the file tomcat-install/bin/catalina.sh or catalina.bat for how this variable is used. For example,

set CATALINA_OPTS=-Xms512m -Xmx512m (Windows, no "" around the value)
export CATALINA_OPTS="-Xms512m -Xmx512m" (ksh/bash, "" around the value)
setenv CATALINA_OPTS "-Xms512m -Xmx512m" (tcsh/csh, "" around the value)

So I replaced the line

set CATALINA_OPTS=

with

set CATALINA_OPTS=-Xms512m -Xmx512m

But I still get the error.

javax.servlet.ServletException: Servlet execution threw an exception

root cause

java.lang.OutOfMemoryError: Java heap space java.lang.reflect.Array.multiNewArray(Native Method) java.lang.reflect.Array.newInstance(Array.java:90) nom.tam.util.ArrayFuncs.newInstance(ArrayFuncs.java:1028) nom.tam.fits.ImageData.read(ImageData.java:259) nom.tam.fits.Fits.readHDU(Fits.java:573) controller.UploadServlet.retreiveFITSFileFields(UploadServlet.java:206) controller.ScanServerFiles.doPost(ScanServerFiles.java:39) javax.servlet.http.HttpServlet.service(HttpServlet.java:637) javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

This question is related to java tomcat heap-memory

The answer is


Your change may well be working. Does your application need a lot of memory - the stack trace shows some Image related features.

I'm guessing that the error either happens right away, with a large file, or happens later after several requests.

If the error happens right away, then you can increase memory still further, or investigate find out why so much memory is needed for one file.

If the error happens after several requests, then you could have a memory leak - where objects are not being reclaimed by the garbage collector. Using a tool like JProfiler can help you monitor how much memory is being used by your VM and can help you see what is using that memory and why objects are not being reclaimed by the garbage collector.


First of all you cannot change the memory settings only for a tomcat application but rather for all tomcat instance.

If you are running tomcat from console (using startup.bat) you'll need to edit catalina.bat and play around with CATALINA_OPTS. For example:

set CATALINA_OPTS=-Xms512m -Xmx512m

Restarting tomcat will apply the new settings.

If you are still getting OutOfMemoryError you need to know how much memory does your application need at that particular moment (nom.tam.util.ArrayFuncs...). You'll either have to optimize the application or simply increase the memory provided to tomcat.


There is a mechanism to do it without modifying any files that are in the distribution. You can create a separate file %CATALINA_HOME%\bin\setenv.bat or $CATALINA_HOME/bin/setenv.sh and put your environment variables there. Further, the memory settings apply to the JVM, not Tomcat, so I'd set the JAVA_OPTS variable instead:

set JAVA_OPTS=-Xmx512m


  • Open the server tab in eclipse
  • right click open
  • click on open lauch configuration
  • Go to arguments
  • Here you can add in VM arguments after endorsed

    -Xms64m -Xmx256m
    

You need to add the following lines in your catalina.sh file.

export CATALINA_OPTS="-Xms512M -Xmx1024M"

UPDATE : catalina.sh content clearly says -

Do not set the variables in this script. Instead put them into a script setenv.sh in CATALINA_BASE/bin to keep your customizations separate.

So you can add above in setenv.sh instead (create a file if it does not exist).


you can set this in catalina.sh as CATALINA_OPTS=-Xms512m -Xmx512m

Open your tomcat-dir/bin/catalina.sh file and add following line anywhere -

CATALINA_OPTS="$CATALINA_OPTS -Xms1024m -Xmx3024m"

and restart your tomcat


Easiest way of doing is: (In Linux/Ububuntu e.t.c)

Go to tomcat bin directory:

cd /opt/tomcat8.5/bin

create new file under bin directory "setenv.sh" and save below mention entries in it.

export CATALINA_OPTS="$CATALINA_OPTS -Xms512m"
export CATALINA_OPTS="$CATALINA_OPTS -Xmx2048m"
export CATALINA_OPTS="$CATALINA_OPTS -XX:MaxPermSize=256m"

and issue command:

./catalina.sh run

In your catalina log file you can see entry like this:

INFO [main] VersionLoggerListener.log Command line argument: -Xms512m
INFO [main] VersionLoggerListener.log Command line argument: -Xmx2048m
INFO [main] VersionLoggerListener.log Command line argument: -XX:MaxPermSize=256m

Which confirms that above changes took place.

Also, the value of "Xms512m" and "-Xmx2048m" can be modified accordingly in the setenv.sh file.

Startup of tomcat could be done in two steps as well. cd /opt/tomcat8.5/bin

Step #1
run ./setenv.sh 
Step #2
./startup.sh

If you're using systemd edit:

/usr/lib/systemd/system/tomcat8.service

and set

Environment=CATALINA_OPTS="-Xms512M -Xmx2048M -XX:MaxPermSize=256m"

Just set this extra line in catalina.bat file

LINE NO AROUND: 143

set "CATALINA_OPTS=-Xms512m -Xmx512m"

And restart Tomcat service


if you are using Windows, it's very simple. Just go to System Environnement variables (right-clic Computer > Properties > Advanced System Parameters > Environnement Variables); create a new system variable with name = CATALINA_OPTS and value = -Xms512m -Xmx1024m. restart Tomcat and enjoy!


For Windows Service, you need to run tomcat9w.exe (or 6w/7w/8w) depending on your version of tomcat. First, make sure tomcat is stopped. Then double click on tomcat9w.exe. Navigate to the Java tab. If you know you have 64 bit Windows with 64 bit Java and 64 bit Tomcat, then feel free to set the memory higher than 512. You'll need to do some task manager monitoring to determine how high to set it. For most apps developed in 2019... I'd recommend an initial memory pool of 1024, and the maximum memory pool of 2048. Of course if your computer has tons of RAM... feel free to go as high as you want. Also, see this answer: How to increase Maximum Memory Pool Size? Apache Tomcat 9


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to tomcat

Jersey stopped working with InjectionManagerFactory not found The origin server did not find a current representation for the target resource or is not willing to disclose that one exists. on deploying to tomcat Spring boot: Unable to start embedded Tomcat servlet container Tomcat 404 error: The origin server did not find a current representation for the target resource or is not willing to disclose that one exists Spring Boot application in eclipse, the Tomcat connector configured to listen on port XXXX failed to start Kill tomcat service running on any port, Windows Tomcat 8 is not able to handle get request with '|' in query parameters? 8080 port already taken issue when trying to redeploy project from Spring Tool Suite IDE 403 Access Denied on Tomcat 8 Manager App without prompting for user/password Difference between Xms and Xmx and XX:MaxPermSize

Examples related to heap-memory

Node.js heap out of memory Set default heap size in Windows how to increase java heap memory permanently? What is the largest possible heap size with a 64-bit JVM? Tomcat 7: How to set initial heap size correctly? Heap space out of memory C: How to free nodes in the linked list? java.lang.OutOfMemoryError: GC overhead limit exceeded How is the default max Java heap size determined? How to increase Java heap space for a tomcat app