JVM "Could not reserve enough space for object heap" - Fix -Xmx in CI
By Daniel Zoghalchali·Latchkey
The JVM failed to start because it could not reserve the heap it was asked for. The requested -Xmx exceeds the memory the OS can give it - too large for the runner’s RAM, or beyond what a 32-bit JVM can address (~2-4 GB).
What this error means
The JVM aborts at startup - before any code runs - with Error occurred during initialization of VM: Could not reserve enough space for <N>KB object heap. It is a launch failure, not an in-run OutOfMemoryError.
JVM output
Error occurred during initialization of VM
Could not reserve enough space for 4194304KB object heap
Common causes
-Xmx exceeds available memory
An -Xmx4g (often from MAVEN_OPTS/org.gradle.jvmargs/JAVA_TOOL_OPTIONS) on a 2 GB runner cannot be reserved, so the JVM refuses to start. Container memory limits count too.
32-bit JVM cannot address the heap
A 32-bit JVM is capped near 2-4 GB of addressable space. Requesting a heap above that ceiling fails to reserve regardless of physical RAM.
How to fix it
Set -Xmx within the runner’s memory
Lower the requested heap so it fits the runner (and any container limit), leaving headroom for the OS.
Terminal
# fits a small runner - leave room for OS + other JVMs
export MAVEN_OPTS="-Xmx1g"
# Gradle:# gradle.properties -> org.gradle.jvmargs=-Xmx1g
Use a 64-bit JDK and check container limits
Confirm a 64-bit JDK (java -version shows "64-Bit Server VM").
Check the container/cgroup memory limit, not just host RAM - -Xmx must fit the limit.
Audit JAVA_TOOL_OPTIONS/MAVEN_OPTS/org.gradle.jvmargs for an oversized -Xmx you did not intend.
How to prevent it
Set -Xmx relative to the runner/container memory, with OS headroom.
Use a 64-bit JDK so large heaps are addressable.
Keep JAVA_TOOL_OPTIONS/MAVEN_OPTS/org.gradle.jvmargs consistent and right-sized.
Frequently asked questions
What causes ""Could not reserve enough space for ... object heap""?
An -Xmx4g (often from MAVEN_OPTS/org.gradle.jvmargs/JAVA_TOOL_OPTIONS) on a 2 GB runner cannot be reserved, so the JVM refuses to start. Container memory limits count too.
How do I fix "Could not reserve enough space for ... object heap"?
Lower the requested heap so it fits the runner (and any container limit), leaving headroom for the OS.
Can Latchkey fix this automatically?
Yes. Latchkey runs your GitHub Actions on managed runners that detect this failure, apply the fix, and retry the job automatically - self-healing is on by default.