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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With