How is the default maximum heap size calculated for Oracle Java 7 JVM?

According to the default GC, the default maximum heap size should be "Less than 1/4 of physical memory or 1 GB".

Reading that I would expect jvm on a server-grade machine with 96GB of RAM to have the default maximum heap size 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, if I read the documentation correctly, the default maximum heap size should be no more than 1 GB, but in practice it is about 1/4 of the server memory. Why?

+3


source to share


1 answer


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

default maximum heap size should be "Less than 1/4" of physical memory. In your case, the 1/4 main memory is 24GB and the heap size is 21GB, which suits your first line.

To make it clearer, run below code to get the actual size of main memory

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 that your HEAP SIZE is 1 / 4th of your main memory or maybe slightly less.

+6


source







All Articles