2

I have Java 8 installed in my system and am able to view and set the initial and max heap size for JVM from command line. Am using tomcat 7 and while going through some of the tutorials I found a way to change the heap size for tomcat as well using setenv.bat file.

My question here is how are the above two things different? The start up script or batch file of tomcat uses the java 8 installed in the system using JAVA_HOME environment variable.

If my JVM heap space is 1024 M and I set 512 M heap space for tomcat, does it mean that my tomcat application can use up to 512 M of heap from 1024 M heap of the JVM?

2 Answers 2

5

Memory settings apply to the JVM, not Tomcat

You can create a separate file %CATALINA_HOME%\bin\setenv.bat or $CATALINA_HOME/bin/setenv.sh and put your environment variables there.

so I'd like to set the JAVA_OPTS variable instead:

set JAVA_OPTS=-Xmx512m

For Xmx:

Specifies the maximum size, in bytes, of the memory allocation pool. This value must a multiple of 1024 greater than 2MB. Append the letter k or K to indicate kilobytes, or m or M to indicate megabytes. The default value is 64MB. The upper limit for this value will be approximately 4000m on Solaris 7 and Solaris 8 SPARC platforms and 2000m on Solaris 2.6 and x86 platforms, minus overhead amounts. So, in simple words, you are saying Java to use Maximum of 1024 MB from available memory.

NOTE: there is NO SPACE between -Xmx and 1024m

Resource Link:

  1. How to Change JVM Heap Setting (-Xms -Xmx) of Tomcat – Configure setenv.sh file – Run catalina.sh
  2. How to Increase Apache Tomcat HeapSize (JVM Heap) in Eclipse IDE (integrated development environment) to Avoid OutOfMemory

UPDATE1: Setting Up Multiple Tomcat Instances

Multiple Tomcat instances are possible to create with the use of the CATALINA_BASE environment variable. Each instance uses a common binary distribution but uses its own conf, webapps, temp, logs and work directories. Each instance also has its own JVM and, thereby, its own memory pool. If you have defined the maximum memory to be 512MB via JAVA_OPTS, each instance will attempt to allocate a maximum of 512MB.

For more details, you can go through this tutorial: Connecting Apache's Web Server to Multiple Instances of Tomcat

Resource Link:

  1. 5 Scenarios and Best Practices for Running Multiple Instances of Tomcat or tc Server
3
  • I followed the second link you shared, so when I make changes to heap of tomcat in eclipse or I create a setenv.bat file and set the heap size parameters then it would make changes in heap of JVM right? if it's so, what if i have two tomcat servers running using same JVM and I make heap changes to one of the tomcat , wil this change affect the other tomcat server as well?
    – DPH
    Jun 22, 2016 at 11:21
  • Could you please help me understand the above concern
    – DPH
    Jun 23, 2016 at 10:32
  • Thank you for the explaination
    – DPH
    Jun 24, 2016 at 9:58
0

A third way of adjusting the Java memory for Tomcat via Registry

Using regedit, browse to

 HKEY_LOCAL_MACHINE > SOFTWARE > Wow6432Node > ApacheSoftwareFoundation >
   Procrun 2.0 > apache-tomcat > Parameters > Java

and you may set the following keys (data values are in MB here):

 JvmMs  REG_DWORD   0x00000400 (1024)
 JvmMx  REG_DWORD   0x00003800 (14336)

In this example, the Java virtual machine will always (in particular for the Tomcat) use at least 1GB and up to 14GB of memory.

Interestingly, this settings seems to overwrite the memory specifications via CATALINA_OPTS (containing the JAVA_OPTS) when restarting the (Tomcat) service.

I checked JVM's max memory via http://localhost:8080/manager/status (in the section JVM) when restarting the service to observe this.

Related posts with helpful answers:

1
  • I was just told that this approach is equivalent with adjusting the Initial memory pool (for JvMs) and the Maximum memory pool (for JvmMx) on the tab "Java" when double-clicking on tomcat8w.exe which can be found in the respective bin directory of your Tomcat.
    – B--rian
    Aug 7, 2017 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.