Failed to Allocate Heap when HugePage Set to Size Equal or Greater than Max Heap Size


Applies To

Java SE JDK and JRE - Version 8 and later


Symptoms

When allocating a JVM heap with any of the HugePages flags set, with the heap smaller than the HugePage reserved on the system, the JVM will fail with the following error:

$<JAVA_HOME>/bin/java -XX:+UseHugeTLBFS -Xms2g -Xmx2g -version
Java HotSpot(TM) 64-Bit Server VM warning: Failed to reserve large pages memory req_addr: 0x0000000080000000 bytes: 2147483648 (errno =12).
java version "11.0.8" 2020-07-14 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.8+10-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.8+10-LTS, mixed mode)

But the environment has a HugePage area greater than 2 Gb, the heap size used in the example above: (HugePages_Free: 1052) x (Hugepagesize: 2048 kB) = Free huge area: 2154496 kB > 2G (2097152 kB)

# cat /proc/meminfo | grep Huge
AnonHugePages: 0 kB
ShmemHugePages: 0 kB
HugePages_Total: 1052
HugePages_Free: 1052
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB
Hugetlb: 2154496 kB
#

Cause

The HugePage area is smaller than Java VM needs.


Solution

Decrease the heap (-Xmx) below the HugePage size, or increase HugePages to larger than the desired max heap.

The Java SE 11 Tools Reference mentions that Large Pages are used in addition to the Java heap.

Large Pages

Note: If you configure (or resize) the OS kernel parameters /proc/sys/kernel/shmmax or /proc/sys/vm/nr_hugepages, Java processes may allocate large pages for areas in addition to the Java heap. These steps can allocate large pages for the following areas:

  • Java heap
  • Code cache
  • The marking bitmap data structure for the parallel GC

Consequently, if you configure the nr_hugepages parameter to the size of the Java heap, then the JVM can fail in allocating the code cache areas on large pages because these areas are quite large in size.

If a maximum heap size of 1.8 Gb was specified, the warning will not occur.

$<JAVA_HOME>/bin/java -XX:+UseHugeTLBFS -Xms1900m -Xmx1900m -version
Java HotSpot(TM) 64-Bit Server VM warning: Failed to reserve large pages memory req_addr: 0x0000000089400000 bytes: 1992294400 (errno = 12).
java version "11.0.8" 2020-07-14 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.8+10-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.8+10-LTS, mixed mode)
$ <JAVA_HOME>/bin/java -XX:+UseHugeTLBFS -Xms1800m -Xmx1800m -version
java version "11.0.8" 2020-07-14 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.8+10-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.8+10-LTS, mixed mode)

The code cache size is shown below:

$<JAVA_HOME>/bin/java  -XX:+UseHugeTLBFS -Xms1800m -Xmx1800m -XX:+PrintFlagsFinal -version|grep -i ReservedCodeCacheSize
    uintx ReservedCodeCacheSize                    = 251658240                              {pd product} {ergonomic}
java version "11.0.8" 2020-07-14 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.8+10-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.8+10-LTS, mixed mode)$

Hugetlb: 2154496 kB < 2191360 kB = ((MaxHeapSize:1900m=1945600 kB) + (ReservedCodeCacheSize:251658240b=245760 kB))

Hugetlb: 2154496 kB > 2088960 kB = ((MaxHeapSize:1800m=1843200 kB) + (ReservedCodeCacheSize:251658240b=245760 kB))


Last reviewed on Sat Feb 01 2025 00:00:00 GMT+0000 (Coordinated Universal Time)