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?

4
  • What is the Xmx Parameter set to you call the java main with? Commented Dec 18, 2014 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 Commented Dec 18, 2014 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: docs.oracle.com/javase/7/docs/technotes/guides/vm/… Commented Dec 18, 2014 at 12:38
  • @ChristianDietrich there were no Xmx parameters, the question is about the default maximum heap size. Commented Jul 8, 2016 at 5:46

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
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.
The code above is wrong as of Java 8. It gives me total system memory. Here is the right one: stackoverflow.com/a/12807848/4182868
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...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.