Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 631
Jeff Thompson Avatar asked Jan 06 '09 16:01

Jeff Thompson


People also ask

How do you set the maximum and minimum value of JVM memory?

use the arguments -Xms<memory> -Xmx<memory> . Use M or G after the numbers for indicating Megs and Gigs of bytes respectively. -Xms indicates the minimum and -Xmx the maximum. you may want to look at MaxPermSize as well.

Is it good to set the max and min JVM heap size the same?

Larger heap size will cause less frequent, but bigger and more disruptive garbage collection if using the traditional oracle garbage collection algorithm. So bigger is not better. The exact behavior will depend on the JVM implementation.


1 Answers

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 
like image 191
Todd Avatar answered Oct 15 '22 22:10

Todd