Search code examples
javaout-of-memoryjvm-arguments

How to overwrite the -Xmx50m in JVM options?


I was using a tool. The JVM option is configurable.

Recently, I get a OutOfMemoryError so I add -Xmx1024m in the option configuration as below.

The config.properties: JVM_OPTION="-Xmx4096m -XX:-UseGCOverheadLimit"

The cmd log before: java -Xms16777216 -Xmx50m com.f.Startmain

The cmd log now: java -Xms16777216 -Xmx50m com.f.Startmain -Xmx4096m -XX:-UseGCOverheadLimit

The tool is still out of memory now as the -Xmx4096m is not overwrite the old one. The tool is not modifiable, and the configuration is only adding new options at the end of the command.

Could someone know a method to overwrite -Xmx50m in JVM options?


Solution

  • This seems to have been answered here

    apparently for both windows and linux, you can set:

    export _JAVA_OPTIONS="-Xmx1g"
    

    I ran a quick test and this seems to override the commandline params. (unexpected)

    my code:

    public class Test {
      public static void main(String[] argv) {
        System.out.println("mem: " + Runtime.getRuntime().freeMemory());
        System.out.println("total mem: " + Runtime.getRuntime().totalMemory());
      }
    }
    

    This is the output of running this program

    export _JAVA_OPTIONS="-Xmx20m";
    java Test
    >> Picked up _JAVA_OPTIONS: -Xmx20m
    >> mem: 19601168
    >> total mem: 20447232
    
    java -Xmx123m Test
    >> Picked up _JAVA_OPTIONS: -Xmx20m
    >> mem: 19601320
    >> total mem: 20447232
    
    # clear java options
    export _JAVA_OPTIONS=
    java -Xmx123m Test
    >> Picked up _JAVA_OPTIONS: 
    >> mem: 121773128
    >> total mem: 123731968
    

    As you can see, the Xmx value on the commandline is ignored until I clear the _JAVA_OPTIONS.