Java GC Tutorials - Herong's Tutorial Examples - v1.11, by Dr. Herong Yang
Memory Allocation Limits on Windows Systems
This section provides a tutorial example on how JVM expands the Heap space to allocate more objects and leads to the 'java.lang.OutOfMemoryError: Java heap space' Java exception, if it is not allowed to expand any more.
Of course, specifying a higher heap size with the -Xms and -Xmx options will resolve the OutOfMemoryError exception. But the version of the HotSpot JVM and the operating system do have limits on how much memory you can allocate to the JVM heap. Some examples of the heap size limit are listed below.
1. 64-bit HotSpot JVM 17 on a new Windows 10 computer - Rerun the example program provided in the last tutorial with a higher heap size to see how much memory the 64-bit HotSpot JVM can be allocated:
herong> java -version java version "17.0.1" 2021-10-19 LTS Java(TM) SE Runtime Environment (build 17.0.1+12-LTS-39) Java HotSpot(TM) 64-Bit Server VM (build 17.0.1+12-LTS-39, mixed mode, sharing) herong> java -Xms10g HeapMemoryCrash Total memory: 10737418240 Free memory: 10736628632 Used memory: 789608 Press ENTER key to continue: herong> java -Xms11g HeapMemoryCrash Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x0000000540000000, 11811160064, 0) failed; error='The paging file is too small for this operation to complete' (DOS error/errno=1455) # # There is insufficient memory for the Java Runtime Environment to continue. # Native memory allocation (mmap) failed to map 11811160064 bytes # for G1 virtual space # An error report file with more information is saved as: # C:\herong\tmp\hs_err_pid12064.log
This tells me that the 64-bit HotSpot JVM can only allocate 10 GB for the JVM heap space on my new Windows 10 computer.
2. 64-bit HotSpot JVM 12 on an old Windows 10 computer - Rerun the example program provided in the last tutorial with a higher heap size to see how much memory the 64-bit HotSpot JVM can be allocated:
herong> java -version java version "12.0.1" 2019-04-16 Java(TM) SE Runtime Environment (build 12.0.1+12) Java HotSpot(TM) 64-Bit Server VM (build 12.0.1+12, mixed mode, sharing) herong> java -Xms25g HeapMemoryCrash Total memory: 26843545600 Free memory: 26842982168 Used memory: 563432 Press ENTER key to continue: herong> java -Xms26g HeapMemoryCrash Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x000001b8bbbc0000, 436207616, 0) failed; error='The paging file is too small for this operation to complete' (DOS error/errno=1455) # # There is insufficient memory for the Java Runtime Environment to continue. # Native memory allocation (mmap) failed to map 436207616 bytes for # Failed to commit area from 0x000001b8bbbc0000 to 0x000001b8d5bc0000 # of length 436207616. # An error report file with more information is saved as: # C:\herong\hs_err_pid21136.log
This tells me that the 64-bit HotSpot JVM accepts up to 25 GB for the JVM heap space.
3. 64-bit HotSpot JVM 8 on a Windows 7 computer - The 64-bit HotSpot JVM 8 on a Window 7 can actually accept a little bit higher heap size:
herong> java -version java version "1.8.0_161" Java(TM) SE Runtime Environment (build 1.8.0_161-b12) Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode) herong> java -Xms27g HeapMemoryCrash Total memory: 27783069696 Free memory: 27782734072 Used memory: 335624 Press ENTER key to continue: ... herong> java -Xms28g HeapMemoryCrash Error occurred during initialization of VM Unable to allocate 917504KB bitmaps for parallel garbage collection for the requested 29360128KB heap. Error: Could not create the Java Virtual Machine. Error: A fatal exception has occurred. Program will exit.
This tells me that the 64-bit HotSpot JVM only accepts up to 27 GB for the JVM heap space.
4. 32-bit HotSpot JVM 8 on a Windows 7 computer - If you are using the 32-bit HotSpot JVM, it can only a much smaller heap size:
herong> java -version java version "1.8.0_45" Java(TM) SE Runtime Environment (build 1.8.0_45-b15) Java HotSpot(TM) Client VM (build 25.45-b02, mixed mode) herong> java -Xms1550m HeapMemoryCrash Total memory: 1571160064 Free memory: 1570902744 Used memory: 257320 Press ENTER key to continue: ... herong> java -Xms1560m HeapMemoryCrash Error occurred during initialization of VM Could not reserve enough space for 1597440KB object heap
This tells us that the 32-bit HotSpot JVM only accepts up to 1,550 MB for the JVM heap space.
Table of Contents
►Heap Memory Area and Size Control
Java Exception: "java.lang.OutOfMemoryError: Java heap space"
►Memory Allocation Limits on Windows Systems
OutOfMemoryError Comparison of HotSpot and JRockit
JVM Garbage Collection Logging
Introduction of Garbage Collectors
Serial Collector - "+XX:+UseSerialGC"
Parallel Collector - "+XX:+UseParallelGC"
Concurrent Mark-Sweep (CMS) Collector - "+XX:+UseConcMarkSweepGC"
Garbage First (G1) Collector - "+XX:+UseG1GC"
The Z Garbage Collector (ZGC) - "+XX:+UseZGC"
Object References and Garbage Collection
Garbage Collection Performance Test Program
Performance Tests on Serial Collector
Performance Tests on Parallel collector
Performance Tests on Concurrent collector
Performance Tests on G1 collector