Knut Saua Mathiesen
Knut Saua Mathiesen

Reputation: 1130

Java max memory differes whether Xms=Xmx or not

Running the following main-method:

public class Test {
    public static void main(String[] args) {
        System.out.println(Runtime.getRuntime().maxMemory() / (1024.0d * 1024.0d));
    }
}

With:

  1. -Xmx6G results in the output 5461.5
  2. -Xmx6G -Xms6G results in output 5888.0

Why do they differ?

I am running Java HotSpot(TM) 64-Bit Server VM 1.8.0_60 on Windows.

Upvotes: 2

Views: 590

Answers (1)

the8472
the8472

Reputation: 43125

Xmx configures the upper bound of the overall size of the managed heap. That is not the same thing as the allocatable amount of java objects.

This due to the generational heap layout where some parts of the heap will always be unused or only used temporarily. E.g. one of the two survivor spaces is always empty when the collector is not running.

Setting the initial heap size affects other settings, primarily the relative sizes of the generations and thus the amount available for java objects.

To see the actual affected settings you can run

diff <(java -Xmx6G -XX:+PrintFlagsFinal) <(java -Xmx6G -Xms6G -XX:+PrintFlagsFinal)

Upvotes: 1

Related Questions