javaheap-size

Changing maximum java heap size


As it is described in many sources I am trying to use java -Xms512m from the command line in order to set maximum size of heap for java machine to 512 Mb. But something goes wrong and command line shows the following
enter image description here

And, as I checked directly from the program: long heapMaxSize = Runtime.getRuntime().maxMemory(); heapMaxSize is equal to 954728448 which is about 1Gb.


Solution

  • This is called a VM parameter, and must be specified when you start your VM (the JVM).

    You may specify the maximum heap size as you run your program:

    java -Xmx1024m -jar yourJar.jar
    

    Or alternatively, you can set an environment variable on your system JAVA_OPTS and the JVM will respect your settings as it starts:

    JAVA_OPTS="-Xmx1024m"
    

    Regarding your second question, I would refer you to What are Runtime.getRuntime().totalMemory() and freeMemory()? as it clarifies what values are returned by Runtime.getRuntime().maxMemory() and friends.