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

Jeff Thompson picture Jeff Thompson · Jan 6, 2009 · Viewed 195.6k times · Source

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.

Answer

Todd picture Todd · Jan 6, 2009

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