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.
What is the Spring Boot default memory settings?
By default Spring Boot app will use JVM default memory settings.
Default heap size
In case your physical memory size is up to 192 megabytes (MB) then default maximum heap size is half of the physical memory.
In case your physical memory size is more than 192 megabytes then default maximum heap size is one fourth of the physical memory.
For example, if your computer has 128 MB of physical memory, then the maximum heap size is 64 MB, and greater than or equal to 1 GB of physical memory results in a maximum heap size of 256 MB.
The maximum heap size is not actually used by the JVM unless your program creates enough objects to require it. A much smaller amount, called the initial heap size, is allocated during JVM initialization. This amount is at least 8 MB and otherwise 1/64th of physical memory up to a physical memory size of 1 GB.
The maximum amount of space allocated to the young generation is one third of the total heap size.
You can check default values specific to you machine with the following command
Linux:
java -XX:+PrintFlagsFinal -version | grep HeapSize
Windows:
java -XX:+PrintFlagsFinal -version | findstr HeapSize
Reference:https://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/parallel.html#default_heap_size
Default thread stack size
The default thread stack size varies with JVM, OS and environment variables.
To find out what your default thread stack size is on your platform, use
In Linux:
java -XX:+PrintFlagsFinal -version | grep ThreadStackSize
In Windows:
java -XX:+PrintFlagsFinal -version | findstr ThreadStackSize
Increasing the JVM maximum heap size for memory intensive applications
Get yourself a 64-bit JVM from Oracle.
What are the -Xms and -Xmx parameters when starting JVM?
The flag Xmx
specifies the maximum memory allocation pool for a Java Virtual Machine (JVM), while Xms
specifies the initial memory allocation pool.
This means that your JVM will be started with Xms
amount of memory and will be able to use a maximum of Xmx
amount of memory. For example, starting a JVM like below will start it with 256 MB of memory and will allow the process to use up to 2048 MB of memory:
java -Xms256m -Xmx2048m
The memory flag can also be specified in different sizes, such as kilobytes, megabytes, and so on.
-Xmx1024k
-Xmx512m
-Xmx8g
The Xms
flag has no default value, and Xmx
typically has a default value of 256 MB. A common use for these flags is when you encounter a java.lang.OutOfMemoryError
.
When using these settings, keep in mind that these settings are for the JVM's heap, and that the JVM can and will use more memory than just the size allocated to the heap. From Oracle's documentation:
Note that the JVM uses more memory than just the heap. For example Java methods, thread stacks and native handles are allocated in memory separate from the heap, as well as JVM internal data structures.
Increase heap size in Java
You can increase to 2GB
on a 32
bit system. If you're on a 64 bit system you can go higher. No need to worry if you've chosen incorrectly, if you ask for 5g on a 32 bit system java will complain about an invalid value and quit.
As others have posted, use the cmd-line flags - e.g.
java -Xmx6g myprogram
You can get a full list (or a nearly full list, anyway) by typing java -X.
Related Topics
Encrypt and Decrypt with Aes and Base64 Encoding
How to Create a Hashmap with Two Keys (Key-Pair, Value)
Hash String via Sha-256 in Java
When Are Java Strings Interned
How to Get Whole and Fractional Parts from Double in Jsp/Java
How to Add a New Sourceset to Gradle
How to Include SQLite Database in Executable Jar
No Exception While Type Casting with a Null in Java
How to Get the Current Date and Time of Your Timezone in Java
Can't a Swing Component Be Added to Multiple Containers
Counting the Number of Files in a Directory Using Java
How to Check for a Valid Url in Java
Junit Confusion: Use 'Extends Testcase' or '@Test'
Java, How to Get Current Index/Key in "For Each" Loop
Setter Methods or Constructors
Random "Element Is No Longer Attached to the Dom" Staleelementreferenceexception