When a Java program is called inside a compiled binary, there will be no easy access to its command-line to set the JVM flags. This is also the case when a Java program is called in several places inside an application, it is not easy to set the configuration options such as Min and Max heap size in multiple places.
In these situations JAVA_TOOL_OPTIONS environment variable can be used to set JVM configuration options.
export JAVA_TOOL_OPTIONS='-Xms128m -Xmx512m'
To check whether if the values we have set is recognised by JVM, use the below commands as described in earlier post Java Configuration Option to Get All Flags and Its Values That Will Be Used by JVM
Before setting the JAVA_TOOL_OPTIONS environment variable:
java -XX:+PrintFlagsFinal -version 2>/dev/null| grep -iE 'initialheap|maxheapsize'
size_t InitialHeapSize = 50331648 {product} {ergonomic}
size_t MaxHeapSize = 775946240 {product} {ergonomic}
After setting the environment variable,
export JAVA_TOOL_OPTIONS='-Xms128m -Xmx512m'
java -XX:+PrintFlagsFinal -version 2>/dev/null| grep -iE 'initialheap|maxheapsize'
size_t InitialHeapSize = 134217728 {product} {command line}
size_t MaxHeapSize = 536870912 {product} {command line}
You can see the values we have passed have taken effect.
NOTE:
Command line options have precedence over JAVA_TOOL_OPTIONS, so if the same value is already set as command line argument then the values set in JAVA_TOOL_OPTIONS will not have any effect.
export JAVA_TOOL_OPTIONS='-Xms128m -Xmx512m'
java -XX:+PrintFlagsFinal -Xms64m -Xmx128m -version 2>/dev/null| grep -iE 'initialheap|maxheapsize'
size_t InitialHeapSize = 67108864 {product} {command line}
size_t MaxHeapSize = 134217728 {product} {command line}
In above the example, it can be seen that even though higher heap size settings are configured through JAVA_TOOL_OPTIONS, values passed over the command line is used.
Reference: