how to set JAVA_OPTS for Tomcat in Windows?
Asked Answered
H

7

29

I'm trying to set JAVA_OPTS for Tomcat on a Windows machine, but I keep getting an error if I add more than one variable.

For example, this works:

set JAVA_OPTS="-Xms512M"

But this does not:

set JAVA_OPTS="-Xms512M -Xmx1024M"

It results in the error:

Invalid initial heap size: -Xms512M -Xmx1024M
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.

It's as if I can set one variable (-Xmx will also work) but not several.

I'm using the instructed setenv.bat file and my Tomcat is 7.0.35.

Hyperbaric answered 6/2, 2013 at 14:13 Comment(0)
H
38

Apparently the correct form is without the ""

As in

set JAVA_OPTS=-Xms512M -Xmx1024M
Hyperbaric answered 6/2, 2013 at 14:30 Comment(0)
H
27

For Windows, in case the variable value has space(" ") in it, the correct way is actually to place quotes(") before the variable name like:

set "JAVA_OPTS=-Xms512M -Xmx1024M"
Hillary answered 3/1, 2014 at 10:14 Comment(0)
F
12

Try

set JAVA_OPTS=%JAVA_OPTS% -Xms512M -Xmx1024M
Felicity answered 4/7, 2013 at 14:53 Comment(0)
A
12

I like a combination of Gaurav's and user2550946's answer best, but would like to add two more aspects:

  1. Don't use JAVA_OPTS, instead use CATALINA_OPTS. This will be used solely for starting tomcat, not for shutting it down. Typically you want more memory when starting tomcat, but the shutdown process (which just spins up, tells tomcat to shut down and then ends again) doesn't need any specifically tuned resources. In fact, shutdown can even fail if some ridiculous amount of memory is not available from the OS anymore.

  2. On production systems, my recommentation is to claim the maximum allowed memory immediately. Because if you anticipate that the memory will be required sooner or later, you don't want to discover it not being available at 3am in the night - rather when you start up the server. Thus, set -Xmx and -Xms to the same value in production systems. (This makes my aspect 1 even more relevant)

Or, in one line, here's my recommendation:

set "CATALINA_OPTS=%CATALINA_OPTS% -Xms1024M -Xmx1024M"
Anthropophagi answered 14/7, 2014 at 10:20 Comment(2)
is it correct set CATALINA_OPTS in setenv.bat file?Shang
@pikimota: yes - set CATALINA_OPTS in setenv.bat fileTlingit
C
7

It is recommended that you create a file named setenv.bat and place it in the Tomcat bin directory. With this file (which is run by the catalina.bat and catalina.sh scripts), you can change the following Tomcat environment settings with the JAVA_OPTS variable:

You can set the minimum and maximum memory heap size with the

JVM -Xms and -Xmx parameters.

The best limits depend on many conditions, such as transformations that Integrator ETL should execute. For Information Discovery transformations, a maximum of 1 GB is recommended. For example, to set the minimum heap size to 128 MB and the maximum heap size to 1024 MB, use

JAVA_OPTS=-Xms128m -Xmx1024m        

You should set the maximum limit of the PermGen (Permanent Generation) memory space to a size larger than the default. The default of 64 MB is not enough for enterprise applications. A suitable memory limit depends on various criteria, but 256 MB would make a good choice in most cases. If the PermGen space maximum is too low, OutOfMemoryError: PermGen space errors may occur. You can set the PermGen maximum limit with the following JVM parameter

    -XX:MaxPermSize=256m

For performance reasons, it is recommended that the application is run in Server mode. Apache Tomcat does not run in Server mode by default. You can set the Server mode by using the JVM -server parameter. You can set the JVM parameter in the JAVA_OPTS variable in the environment variable in the setenv file.

The following is an example of a setenv.bat file:

set "JAVA_OPTS=%JAVA_OPTS% -Xms128m -Xmx1024m -XX:MaxPermSize=256m -server"
Chau answered 28/2, 2015 at 10:1 Comment(0)
S
1

This is because, the amount of memory you wish to assign for JVM is not available or may be you are assigning more than available memory. Try small size then u can see the difference.
Try:

set JAVA_OPTS=-Xms128m -Xmx512m -XX:PermSize=128m
Salomone answered 15/9, 2015 at 10:3 Comment(0)
B
0
SET JAVA_HOME=C:\Applications\java\java_8
SET PATH=%PATH%;C:\Applications\java\java_8\bin
SET JAVA_OPTIONS=-d64 -Xms128g -Xmx128g
Billmyre answered 11/10, 2016 at 10:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.