Passing command line options to Java [Was: Re: Heap Size Ergonomics in docker containers]

Thomas Schatzl thomas.schatzl at oracle.com
Thu Nov 2 09:09:13 UTC 2017


Hi Ruslan,

On Wed, 2017-11-01 at 11:30 +0100, Ruslan Synytsky wrote:
> Hi Thomas, thank you for the feedback. Please see inline. 
> 
> [...]
> 
> > There is JAVA_TOOL_OPTIONS, which are always prepended to the java
> > VM command line. See the description [0] for more information.
> 
> Indeed this option is close to what I was looking for! But seems
> JAVA_TOOL_OPTIONS does not work for Xmx. Just a quick test with Java
> HotSpot(TM) 64-Bit Server VM (build 9+181, mixed mode)
> 
> Default
> $ java -XX:+PrintFlagsFinal -version | grep MaxHeapSize
>    size_t MaxHeapSize                              = 643825664      
>                          {product} {command line}
> 
> Define Xmx = 2GB via JAVA_TOOL_OPTIONS 
> $ export JAVA_TOOL_OPTIONS="-Xmx2g" 
> $ java -XX:+PrintFlagsFinal -version | grep MaxHeapSize
> Picked up JAVA_TOOL_OPTIONS: -Xmx2g
>    size_t MaxHeapSize                              = 643825664      
>                          {product} {command line}
> 
> It says that Xmx has been picked up, but in reality it has not been
> applied.  

Hmm, it seems to work here.

E.g.

$ java -XX:+PrintFlagsFinal HelloWorld | grep MaxHeap
   size_t MaxHeapSize                              = 3072327680
                                     {product} {ergonomic}
$ JAVA_TOOL_OPTIONS=-Xmx100m java -XX:+PrintFlagsFinal HelloWorld |
grep MaxHeap
Picked up JAVA_TOOL_OPTIONS: -Xmx100m
   size_t MaxHeapSize                              = 104857600
                                     {product} {command line}
$ echo $JAVA_TOOL_OPTIONS

$ export JAVA_TOOL_OPTIONS=-Xmx10m
$ java -XX:+PrintFlagsFinal HelloWorld | grep MaxHeap
Picked up JAVA_TOOL_OPTIONS: -Xmx10m
   size_t MaxHeapSize                              = 10485760
                                 {product} {command line}

(linux-x64, same hotspot build)
(Also tried with surrounding quotes)
(It does not matter whether you specify a java class on the command
line or not).

There is a note in the documentation that prevents use of it under
particular circumstances, but then according to the code the VM would
not print the "Picked up" line. 

I haven't seen an issue reported with that anywhere either.

> 
> Define Xmx = 3GB via _JAVA_OPTIONS 
> $ export _JAVA_OPTIONS="-Xmx3g" 
> $ java -XX:+PrintFlagsFinal -version | grep MaxHeapSize
> Picked up JAVA_TOOL_OPTIONS: -Xmx2g
> Picked up _JAVA_OPTIONS: -Xmx3g
>    size_t MaxHeapSize                              = 3221225472      
>                         {product} {command line}
> 
> Now it works as expected. However, another issue is that
> _JAVA_OPTIONS is undocumented and hotspot-specific?...

I did not mention _JAVA_OPTIONS because I thought it wouldn't fit your
requirements and overwrites existing options. Also it's undocumented as
you noted.

There is another way to specify multiple java arguments via argument
files using  the "@" parameter which in some cases may be more useful
than the other options.

Thanks,
  Thomas



More information about the hotspot-gc-dev mailing list