Java Top

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE

1. Overview

In this tutorial, we'll learn the cause of “Could not reserve enough space for object heap” error, while going through some possible scenarios.

2. Symptoms

“Could not reserve enough space for object heap” is a specific JVM error that is raised when Java process cannot create the virtual machine due to memory constraints encountered on the running system:

java -Xms4G -Xmx4G -jar HelloWorld.jar

Error occurred during initialization of VM
Could not reserve enough space for object heap
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.

Generally, there are two possible scenarios when we encounter the error.
Firstly, when we lunch a Java process with max heap size limit parameter (-Xmx) and the value is more than what the process can have on the operating system.

The heap size limit varies based on several constraints:

  • hardware architecture (32/64 bit)
  • JVM bit version (32/64 bit)
  • the operating system that we use

Secondly, when the Java process is not able to reserve the specified amount of memory due to other applications that run on the same system and consume memory.

3. Heap Size

Java heap space is the memory allocation pool for the runtime Java program, managed by the JVM itself. By default, the allocation pool is restricted to the initial and maximum size. To learn more about Heap Space in Java, have a look at this article here.

Let's see what the maximum heap size is in different environments and how we can set the limits.

3.1. Max Heap Size

The maximum theoretical heap limit for the 32-bit and 64-bit JVM is easy to determine by looking at the available memory space, 2^32 (4 GB) for 32-bit JVM and 2^64 (16 Exabytes) for 64-bit JVM.

In practice, due to various constraints, the limit can be much lower and varies given the operating system. For example, on 32-bit Windows systems the maximum heap size range is between 1.4 GB to 1.6 GB. In contrast, on 32-bit Linux systems, the maximum heap size can stretch up to 3 GB.

For this reason, if the application requires a large heap we should use the 64-bit JVM. However, with a large heap, the garbage collector will have more work to do, so it's important to find a good balance between heap size and performance.

3.2. How To Control Heap Size Limits?

We have two options to control the heap size limits of a JVM.

First, by using Java command line parameters at each JVM initialization:

-Xms<size>    Sets initial Java heap size. This value must be a multiple of 1024 and greater than 1 MB.
-Xmx<size>    Sets maximum Java heap size. This value must be a multiple of 1024 and greater than 2 MB.
-Xmn<size>    Sets the initial and maximum size (in bytes) of the heap for the young generation.

For the size value, we may append letter k or K, m or M and g or G to indicate kilobytes, megabytes, and gigabytes respectively. If no letter is specified, the default unit (byte), is used.

-Xmn2g
-Xmn2048m
-Xmn2097152k
-Xmn2147483648

Secondly, by using environment variable JAVA_OPTS to configure above Java command line parameters globally. Due to this, each JVM initialization on the system will automatically use the configurations set in the environment variable.

JAVA_OPTS="-Xms256m -Xmx512m"

For more information, check out our comprehensive JVM Parameters guide.

4. Conclusion

In this tutorial, we discussed two possible scenarios when JVM is not able to reserve enough space for object heap. We also learned how to control the heap size limits in order to mitigate this error.

Next, learn more about potential memory issues at runtime and how to identify them.

Java bottom

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE
Comments are closed on this article!