Skip to content
javahacks.dev
Debugging

Fix Java Could Not Reserve Enough Space for Object Heap

Fix the JVM start-up error about reserving heap space by checking 32-bit JVMs, a heap larger than memory, container limits and fragmented address space.

7 min read
Summarize in AI
Flat illustration of a stack trace of frames with a bug beside it, with the OpenJDK, Docker, IntelliJ IDEA and Sentry logos.

The JVM does not start, and it says so before running a single line of your code:

Text
Error occurred during initialization of VM
Could not reserve enough space for 4194304KB object heap

The JVM asked the operating system for a contiguous block of address space for the heap and was refused. Nothing is wrong with your application. The request is bigger than what the process is allowed or able to have, and the cause is one of five: a 32-bit JVM that cannot address that much, flags asking for more memory than the machine has, a container limit smaller than the heap, other processes holding the memory, or an operating-system limit on the process. This post tells them apart with the command that confirms each, then shows how to ask the JVM what it will actually use. Examples use JDK 25 on Linux; the macOS and Windows behavior differs in small ways noted along the way.

Cause 1: a 32-bit JVM

A 32-bit process has at most 4 GiB of address space, and the JVM, the operating system and libraries take part of it. A heap of 2 GiB or more often cannot be reserved as one block. This was the classic cause on older Windows servers and is rare now, but it still appears on legacy installations and mixed-architecture containers. Check the JVM you are running:

Shell
java -version
Text
openjdk version "25.0.1" 2025-10-21
OpenJDK 64-Bit Server VM (build 25.0.1+9, mixed mode, sharing)

The words 64-Bit Server VM are what you want. A 32-Bit or Client VM line means the JVM cannot address a large heap; install a 64-bit JDK and fix JAVA_HOME to point at it. On a 64-bit machine with a 32-bit JVM, the fix is the other binary, not a flag. If your PATH finds a different java than you expect, which -a java lists them all.

Cause 2: the flags ask for more than exists

The simplest cause is a heap setting larger than the memory available. -Xmx sets the maximum and -Xms the initial size; a large -Xms makes the JVM commit that memory at startup, which is where it fails:

Shell
java -Xms16g -Xmx16g -jar app.jar
Text
Error occurred during initialization of VM
Could not reserve enough space for 16777216KB object heap

On a machine with 8 GiB, this cannot work. Find what is available:

Shell
free -m
Text
              total        used        free      shared  buff/cache   available
Mem:           7953        3120        1204         211        3628        4501

The available column (4,501 MiB) is what could be given to a new process without swapping, not free. Set -Xmx below it, with room for non-heap memory (metaspace, thread stacks, code cache, direct buffers), which can add several hundred megabytes. A heap of about 60 to 75 percent of available memory is a sane ceiling for a service sharing a machine. Setting -Xms equal to -Xmx is common for servers and means the whole amount must be available at startup, so lower -Xms if the machine is tight; the JVM then grows the heap as it needs. Also check for flag collisions: JAVA_TOOL_OPTIONS, JDK_JAVA_OPTIONS and a launcher script may each add -Xmx, and the last one wins. The JVM prints a line when it picks them up:

Text
Picked up JAVA_TOOL_OPTIONS: -Xmx16g

If you did not set that, find who did. Flag reference: the java command documentation.

Cause 3: container limits

In a container, memory is capped by a cgroup limit, and the JVM sizes itself from it. A fixed -Xmx larger than the limit fails or, worse, starts and is killed later. Read the limit the JVM sees:

Shell
cat /sys/fs/cgroup/memory.max
Text
536870912

That is 512 MiB. A -Xmx1g in this container exceeds the limit, and the process either cannot reserve the space or is killed by the kernel once it grows past it; the later failure is a different symptom, covered in a Java container killed by the OOM killer. The fix is to size the heap from the limit instead of a fixed number:

Dockerfile
ENTRYPOINT ["java", "-XX:MaxRAMPercentage=70", "-jar", "app.jar"]

The JVM then reads the cgroup limit and takes 70 percent of it. The settings, their defaults and how to verify them are in MaxRAMPercentage and container awareness. Remember that heap is not the whole process: the budget of areas beyond it is laid out in JVM memory areas. In Kubernetes, check that the pod's memory limits is what you think it is (kubectl describe pod), since the limit set in the manifest, not the node's size, is what the JVM sees.

Cause 4: other processes hold the memory

The request can be reasonable and still fail because the machine is full. Another JVM, a database, a browser or leaked processes from earlier runs hold the memory the new one wants. Look at who is using it:

Shell
ps -eo pid,rss,comm --sort=-rss | head -6
Text
  PID   RSS COMMAND
 2210 3904112 java
 1893 1204884 postgres
 3041  611204 node

The first line is a JVM holding about 3.9 GiB resident. If that is a stale process from a previous run of the same application, stop it (kill <pid>) and retry. On a development machine, a debugger session or a forgotten test run leaves processes behind. Beware of overcommit: Linux lets the JVM reserve address space beyond physical memory in many configurations, so a reservation may succeed and the process die later; this is the reason a failure at startup is the better outcome, and why setting the sizes honestly matters. If the Mem line shows plenty of memory but the reservation still fails, the next cause is more likely.

Cause 5: ulimits and cgroups

Operating-system limits can cap the address space or memory of a process below the physical amount. Check the shell's limits:

Shell
ulimit -a | grep -E 'virtual memory|max memory size'
Text
max memory size             (kbytes, -m) unlimited
virtual memory              (kbytes, -v) 4194304

A virtual memory limit of 4,194,304 KiB (4 GiB) caps the total address space of the process, and the JVM reserves far more address space than it uses: the heap, plus compressed class space (1 GiB by default), plus the code cache and mapped libraries. A 3 GiB heap in a process limited to 4 GiB of address space fails with the reserve error even though the physical RAM is free. Raise the limit for the service (ulimit -v unlimited, or LimitAS=infinity in a systemd unit), or lower the heap. In a systemd service, MemoryMax and MemoryHigh apply a cgroup limit the same way a container does, and the JVM reads it as in cause 3. On a shared host, resource managers (Kubernetes, YARN, a batch scheduler) may impose limits that you only see by reading their configuration.

Transparent huge pages and a very large -Xms with -XX:+AlwaysPreTouch are another source: pretouching commits every page at startup, so the whole heap must be physically available at once. Remove AlwaysPreTouch on a small machine.

Verify what the JVM will use

Stop guessing and ask. The JVM can print its final computed values without running your application:

Shell
java -Xmx512m -XX:+PrintFlagsFinal -version | grep -E 'MaxHeapSize|InitialHeapSize|UseContainerSupport'
Text
   size_t InitialHeapSize   = 33554432     {product} {ergonomic}
   size_t MaxHeapSize       = 536870912    {product} {command line}
     bool UseContainerSupport = true       {product} {default}

MaxHeapSize is the number the JVM will try to reserve, and the bracket after it says where the value came from: command line for your flag, ergonomic for a computed default. Change one thing and rerun until the reservation is what you intend. A second check, from inside a running JVM, gives the same figures to log at startup:

java
Runtime rt = Runtime.getRuntime();
System.out.printf("max=%d MiB, total=%d MiB, processors=%d%n",
        rt.maxMemory() >> 20, rt.totalMemory() >> 20, rt.availableProcessors());

Compare max with the machine or container limit, and leave headroom. For the follow-on problem, a heap that is reserved fine and then fills up, the symptoms are in GC overhead limit exceeded and the broader HotSpot troubleshooting guide.

Where this does not apply

If the message names a different area (Could not allocate metaspace, unable to create native thread, Compressed class space), the heap is not the issue: the first points at class metadata, the second at the process or thread limit (ulimit -u, the cgroup pids.max), the third at address space for classes. If the JVM starts and later dies with OutOfMemoryError, the reservation was fine and the application needs more memory or has a leak. And an immediate exit with code 137 and no message is the kernel's OOM killer, not the JVM, which is a container memory problem to fix with limits and sizing.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.