95

How do I set Java's min and max heap size through environment variables?

I know that the heap sizes can be set when launching java, but I would like to have this adjusted through environment variables on my server.

trincot
  • 317,000
  • 35
  • 244
  • 286
Jeff Thompson
  • 2,662
  • 2
  • 18
  • 17
  • The accepted answer is out of date and does not apply anymore. Refer to @Gauthier's answer if you end up here – Layman Aug 12 '20 at 16:30

7 Answers7

92

You can't do it using environment variables directly. You need to use the set of "non standard" options that are passed to the java command. Run: java -X for details. The options you're looking for are -Xmx and -Xms (this is "initial" heap size, so probably what you're looking for.)

Some products like Ant or Tomcat might come with a batch script that looks for the JAVA_OPTS environment variable, but it's not part of the Java runtime. If you are using one of those products, you may be able to set the variable like:

set JAVA_OPTS="-Xms128m -Xmx256m"  

You can also take this approach with your own command line like:

set JAVA_OPTS="-Xms128m -Xmx256m"  
java ${JAVA_OPTS} MyClass
nickb
  • 59,313
  • 13
  • 108
  • 143
Todd
  • 2,321
  • 14
  • 11
  • 1
    The `JAVA_OPTS` technique is also used by the scripts generated by the maven appassemble plugin. – Mutant Bob Mar 17 '18 at 05:11
  • 3
    as @neves mentioned in a comment below this has changed in java 7 (https://docs.oracle.com/javase/7/docs/webnotes/tsg/TSG-VM/html/envvars.html). you can use JAVA_TOOL_OPTIONS (see link and answers below) – keisar Jul 10 '18 at 16:33
76

If you want any java process, not just ant or Tomcat, to pick up options like -Xmx use the environment variable _JAVA_OPTIONS.

In bash: export _JAVA_OPTIONS="-Xmx1g"

wmli
  • 1,600
  • 14
  • 15
  • 2
    Perfect, this works great and saved me a lot of looking. – rodnaph Apr 15 '15 at 10:19
  • 3
    In Windows: `set _JAVA_OPTIONS=-Xmx1g` To make it permanent, [make a new environment variable](http://superuser.com/questions/949560/how-do-i-set-system-environment-variables-in-windows-10) `_JAVA_OPTIONS` and set it to `-Xmx1g` – tim_hutton Feb 02 '16 at 22:36
  • 3
    See also [Difference between _JAVA_OPTIONS and JAVA_TOOL_OPTIONS](http://stackoverflow.com/questions/28327620/difference-between-java-options-java-tool-options-and-java-opts/40736821#40736821) – Vadzim Mar 15 '17 at 17:02
34

You can use JAVA_TOOL_OPTIONS.

Example:

export JAVA_TOOL_OPTIONS=-Xmx512m

It has been mentioned in some comments, and in another answer.

The OP's question is quite old, but as it is the first google result for the question, I thought i would add the answer here for clarity's sake.

gotson
  • 3,613
  • 1
  • 23
  • 40
17

Actually, there is a way to set global defaults for Sun's JVM via environment variables.

See How to set a java system property so that it is effective whenever I start JVM without adding it to the command line arguments.

Community
  • 1
  • 1
Vadzim
  • 24,954
  • 11
  • 143
  • 151
9

You can't do it using environment variables. It's done via "non standard" options. Run: java -X for details. The options you're looking for are -Xmx and -Xms (this is "initial" heap size, so probably what you're looking for.)

a2800276
  • 3,272
  • 22
  • 33
  • 2
    It looks it changed in Java 7: http://docs.oracle.com/javase/7/docs/webnotes/tsg/TSG-VM/html/envvars.html – neves Aug 03 '17 at 22:19
4

I think your only option is to wrap java in a script that substitutes the environment variables into the command line

frankodwyer
  • 13,948
  • 9
  • 50
  • 70
3

A couple of notes:

  1. Apache ant doesn't know anything about JAVA_OPTS, while Tomcat's startup scripts do. For Apache ant, use ANT_OPTS to affect the environment for the JVM that runs /ant/, but not for the things that ant might launch.

  2. The maximum heap size you can set depends entirely on the environment: for most 32-bit systems, the maximum amount of heap space you can request, regardless of available memory, is in the 2GiB range. The largest heap on a 64-bit system is "really big". Also, you are practically limited by physical memory as well, since the heap is managed by the JVM and you don't want a lot of swapping going on to the disk.

  3. For server environments, you typically want to set -Xms and -Xmx to the same value: this will fix the size of the heap at a certain size and the garbage collector has less work to do because the heap will never have to be re-sized.

Christopher Schultz
  • 20,221
  • 9
  • 60
  • 77