Skip to content
Latchkey

Bazel server JVM OutOfMemoryError (heap) in CI

The Bazel server runs on a JVM, and analyzing a large build graph can exhaust its heap. When it does, Bazel crashes with an OutOfMemoryError from com.google.devtools.build.lib. Raise the server heap with --host_jvm_args=-Xmx.

What this error means

A build aborts with "java.lang.OutOfMemoryError: Java heap space" in a com.google.devtools.build.lib stack, sometimes after the server appears to hang.

bazel
FATAL: bazel crashed due to an internal error.
java.lang.OutOfMemoryError: Java heap space
  at com.google.devtools.build.lib.skyframe...

Common causes

The server heap is too small for the graph

A large monorepo graph needs more heap than the default the Bazel server starts with, so analysis exhausts it.

A memory-constrained CI runner

Small runners give the JVM little headroom, so the server hits the heap ceiling on builds that pass on larger machines.

How to fix it

Raise the Bazel server heap

Set the JVM max heap for the Bazel server with a startup flag in .bazelrc so it applies to every command.

.bazelrc
startup --host_jvm_args=-Xmx4g

Use a larger runner or trim analysis

  1. Move the job to a runner with more memory so the JVM has headroom.
  2. Reduce the analyzed set with narrower target patterns where possible.
  3. Re-run so analysis fits within the heap.
Terminal
bazel --host_jvm_args=-Xmx6g build //foo/...

How to prevent it

  • Set --host_jvm_args=-Xmx in .bazelrc sized to your graph.
  • Give CI runners enough memory for the Bazel server plus actions.
  • Build narrower target patterns when the full graph is not needed.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →