Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:


November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:


Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

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.


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.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:


Generic footer banner
Comments are closed on this article!