simaremare
simaremare

Reputation: 417

Java -Xms and -Xmx test

i make a class, Main which contains the main method like this:

public class Main {

    public static void main(String[] _args) {
    System.out.println("free:" + Runtime.getRuntime().freeMemory()
        + ". max:" + Runtime.getRuntime().maxMemory());
    }
}

then i run the class with -Xms2m and -Xmx2m. the output is like this:

free:2984840. max:7471104

then, my question is, if a megabytes is 1048576bytes, then the shown free memory is not 2megabytes as set in the -Xms. when i increase the -Xms option to 3m, it shows a megabyte addition free. anyone can explain what is happening,.?

a different case occurs with -Xmx option, when i increase it to 3m, 4m, and 5m, the max is keep showing 7471104. which is around 71.44m. i don't really understand how this two options work.

this is what is printed when i set -Xms3m and -Xmx6m

free:4033416. max:7471104

anyone can help,.?

thank you :)

notes: i am working with Win7 x64 and Java 1.6 update 35.

Upvotes: 1

Views: 2070

Answers (2)

Aleš
Aleš

Reputation: 9028

The heap not only contains data allocated by your application but also objects allocated by the VM (e.g. objects representing the classes). The Hotspot VM footprint is definitely bigger than what you set as max heap size. That is the reason why the actual heap size is bigger.

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533880

The JVM has restrictions in terms of how memory can be allocated which is not obvious. This means it only takes the sizes you give it as a hint which will be followed as strictly as it can. For very small memory sizes these differences are more dramatic but even if you ask for 20 GB it won't be precisely 20 GB to the byte.

Upvotes: 1

Related Questions