How Is the Default Max Java Heap Size Determined

What is the default max heap size (-Xmx) in Java 8?

It varies on implementation and version, but usually it depends on the VM used (e.g. client or server, see -client and -server parameters) and on your system memory.

Often for client the default value is 1/4th of your physical memory or 1GB (whichever is smaller).

Also Java configuration options (command line parameters) can be "outsourced" to environment variables including the -Xmx, which can change the default (meaning specify a new default). Specifically the JAVA_TOOL_OPTIONS environment variable is checked by all Java tools and used if exists (more details here and here).

You can run the following command to see default values:

java -XX:+PrintFlagsFinal -version

It gives you a loooong list, -Xmx is in MaxHeapSize, -Xms is in InitialHeapSize. Filter your output (e.g. |grep on linux) or save it in a file so you can search in it.

Why default java max heap is 1/4th of Physical memory?

This dates back to JDK 5, which introduced JVM ergonomics. Prior to this, the JVM would set very small defaults for the heap space. JDK 1.1 had a default of 16Mb for both Xms and Xmx, JDK 1.2 changed this to Xms of 1Mb and Xmx of 64Mb by default. In JDK 1.3, Xms default increased to 2Mb.

Since Java was proving more popular on servers and memory capacities were increasing significantly, Sun introduced the concept of a server-class machine in JDK 5. This is one that has 2 or more physical processors and 2 or more Gb of memory (if I remember rightly, in JDK 5, the machine also had to not be running Windows to count as a server).

On server-class machines by default, the following parameters were set

  • Throughput garbage collector (i.e. the parallel collector)
  • initial heap size of 1/64 of physical memory up to 1Gbyte
  • maximum heap size of 1/4 of physical memory up to 1Gbyte
  • Server runtime compiler

Ergonomics provided two command-line flags that allowed a user to set a performance goal for the JVM; the idea being that the JVM would then figure out internally how to achieve this goal by modifying its parameters. The ultimate goal was to eliminate a lot of the -XX flags that were being used to tune JVM performance manually.

The parameters are:

-XX:MaxGCPauseMillis=nnn which sets the maximum pause time you want for GC in milliseconds.

-XX:GCTimeRatio= which sets the ratio of garbage collection time to application time being 1 / (1 + nnn). This was referred to as the throughput goal.

You can specify either of these goals or both. If the JVM manages to achieve both of these goals it then attempts to reduce the memory being used (the footprint goal).

There's more detail here:

https://www.oracle.com/technetwork/java/ergo5-140223.html

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

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.

Default Java Heap Size and Memory Issues it can cause

Does the default heap size extend itself if memory requirement
increases for the app?

Yes, but only up to the value in -Xmx (or the default for that value that the JVM chose).

If memory was getting full, why was GC not getting triggered?

GC was getting triggered, but it could not collect any garbage. Everything in your heap was non-garbage.

Increase default max heap space in Eclipse

You can set default arguments for a JVM used to run Java apps in the Preferences. Go to the 'Java > Installed JREs' page. Select the JRE/JDK you are using and click the 'Edit...' button. In the edit dialog there is a 'Default VM arguments' field where you can put your settings.

Note: The eclipse.ini lines only change the settings for Eclipse itself, they aren't relevant to running Java apps from Eclipse.

What is the default maximum heap size for Sun's JVM from Java SE 6?

java 1.6.0_21 or later, or so...

$ java -XX:+PrintFlagsFinal -version 2>&1 | grep MaxHeapSize
uintx MaxHeapSize := 12660904960 {product}

It looks like the min(1G) has been removed.

Or on Windows using findstr

C:\>java -XX:+PrintFlagsFinal -version 2>&1 | findstr MaxHeapSize

Why is the deafult maximum heap size so small

The heap is maxed at the lowest of either 1Gb or 1/4th of the total physical memory. Why is that?

Because 64MB was found to be too little on modern machines.

Should modern web servers really never use more than 1GB of heap?

There is no reason to take the default value of a general purpose executable as having anything much to say about best practice for web servers.

Why should 3/4 of the machine's memory not be used?

The assumption is that java isn't the only process on the machine. If it is the most important process on the machine and requires more memory, the user can change the default.



Related Topics



Leave a reply



Submit