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. 32-bit HotSpot JVM on a 32-bit Windows system - Rerun the example with a higher heap size to see how much memory the 32-bit HotSpot JVM can be allocated:

C:\herong>\local\jdk\bin\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)

C:\herong>\local\jdk\bin\java -Xms1550m HeapMemoryCrash
Total memory: 1571160064
 Free memory: 1570902744
 Used memory: 257320
Press ENTER key to continue:
...

C:\herong>\local\jdk\bin\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.

2. 64-bit HotSpot JVM on a 64-bit Windows system - Rerun the example with a higher heap size to see how much memory the 64-bit HotSpot JVM can be allocated::

C:\herong>\local\jdk\bin\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)

C:\herong>\local\jdk\bin\java -Xms27g HeapMemoryCrash
Total memory: 27783069696
 Free memory: 27782734072
 Used memory: 335624
Press ENTER key to continue:
...

C:\herong>\local\jdk\bin\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 us that the 64-bit HotSpot JVM only accepts up to 27 GB for the JVM heap space.

Last update: 2018.

Table of Contents

 About This Book

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"

 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

 Garbage Collection Performance Test Summary

 Outdated Tutorials

 References

 Full Version in PDF/EPUB