4

According to GC ergonomics the default maximum heap size should be "Smaller of 1/4th of the physical memory or 1GB".

Reading that I would expect a jvm on a server-class machine with 96GB ram to have a default maximum heap size of 1GB (the smaller of 96GB/4 = 24GB or 1GB).

However when I compile and run the following code it writes out 21463 (i.e. about 21GB).

public class Main{
        public static void main(String[] args) {
                System.out.println(Runtime.getRuntime().maxMemory() / 1024 / 1024);
        }
}

In case it matters: java -version produces

java version "1.7.0_45"
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)

So to summarize, if I read the documentation correctly the default maximum heap size should be no larger than 1GB, but in practice it's about 1/4 of server memory. How come?

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
user1372408
  • 426
  • 1
  • 4
  • 10
  • What is the Xmx Parameter set to you call the java main with? – Christian Dietrich Dec 18 '14 at 11:37
  • 1
    It also states: The boundaries and fractions given for the heap size are correct for J2SE 5.0. They are likely to be different in subsequent releases as computers get more powerful. . maybe they did simply forgot to adapt the docs – Christian Dietrich Dec 18 '14 at 11:45
  • Well the document does mention that this is specifically the case when the Parallel Garbage Collector is used; is that one configured? The release notes of Java 7 specifically mention that the default parameters have changed also: https://docs.oracle.com/javase/7/docs/technotes/guides/vm/enhancements-7.html – Gimby Dec 18 '14 at 12:38
  • @ChristianDietrich there were no Xmx parameters, the question is about the default maximum heap size. – user1372408 Jul 08 '16 at 05:46

2 Answers2

6

The answer was in your question only in the first line itself -

default maximum heap size should be "Smaller of 1/4th" of the physical memory. In your case 1/4th of main memory is 24GB but heap size is 21GB, which is satisfying your first line statement.

To make it more clear run below code to get the actual main memory size

public class SizeOfMainMemory {

    public static void main(String[] args) {
        com.sun.management.OperatingSystemMXBean mxbean = (com.sun.management.OperatingSystemMXBean) ManagementFactory
                .getOperatingSystemMXBean();
        System.out.println(mxbean.getTotalPhysicalMemorySize()/1024/1024);
    }

}

You will find your HEAP SIZE is 1/4th of your main memory or may be little less.

Bector
  • 1,324
  • 19
  • 35
  • 4
    As a native English speaker, the statement in the documentation means "1GB, or 1/4 of the total physical memory if that would be less than 1GB". The phrase is "the smaller of X or Y", i.e. whichever of the two options has the smaller value. – Ian Roberts Dec 18 '14 at 12:48
  • That is correct and I believe that is how the client JVM used to behave (and perhaps still does?); the server JVM can claim much more. But I must admit that I can't find any hard evidence of how it specifically works in Java 7/8. – Gimby Dec 18 '14 at 15:24
  • The code above is wrong as of Java 8. It gives me total system memory. Here is the right one: http://stackoverflow.com/a/12807848/4182868 – Desik Jul 14 '16 at 18:41
0

OK I might have found an answer.

That linked page mention java "SE-5.0" and makes no reference to "64-bit" perhaps it was oudated?

Here's an updated equivalent:

https://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/ergonomics.html

with a new link in it "For initial heap and maximum heap sizes for 64-bit systems, see the section Default Heap Size in The Parallel Collector."

On that page: https://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/parallel.html#default_heap_size

It states "On 64-bit JVMs, the default maximum heap size can be up to 32 GB if there is 128 GB or more of physical memory."

But the same seems to be accurate for JDK 7 so I'm thinking that link was outdated...

rogerdpack
  • 62,887
  • 36
  • 269
  • 388