published fresh until

Maven: stop Java heap crashes during a build

Summary For Maven 3.9.9 on Java 21, identify whether the heap failure belongs to Maven, a Surefire test fork or a forked compiler. Change that JVM’s limit rather than assuming one setting reaches every process. Maven’s own limit belongs in MAVEN_OPTS.

Not for Docker exit 137 without a Java exception; see docker-container-exit-137-out-of-memory-or-stopped.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

java.lang.OutOfMemoryError: Java heap space A JVM participating in the Maven build cannot allocate another object within its Java heap. Do first: repeat the failing Maven goal with -e and identify the owning process from the failing plugin and stack trace: Maven itself, a forked test JVM, or forked javac. Maven 3.9.9 CLI. Evidence: official references establish which setting controls each JVM; a public Maven build report establishes that increasing a Docker service allocation alone can leave this exception unchanged. Not for: Docker exit 137 without a Java exception; see docker-container-exit-137-out-of-memory-or-stopped.

Diagnostic: java.lang.OutOfMemoryError: Java heap space

As of November 2026, this case is scoped to Maven 3.9.9 on Java 21, with the plugin versions below. These are explicit version examples, not a recommendation to downgrade a newer build. Java 21's memory guide distinguishes insufficient heap from retained objects; this suffix alone does not prove a leak.

1. Maven itself owns the failing heap, including an in-process compiler. For Maven 3.9.9, add an example budget such as -Xmx2g to .mvn/jvm.config, or set it through MAVEN_OPTS in the environment launching Maven. Preserve existing JVM flags. The Maven configuration reference distinguishes JVM options from Maven command arguments. Repeat the same failing goal and compare peak memory and completion. If an existing environment override supplies another -Xmx, reconcile it before judging the project file's effect.

2. The exception belongs to a Surefire test fork. With Maven 3.9.9 and Surefire 3.5.4, add the measured heap budget to that plugin's <argLine>; for a minimal configuration without existing agents, <argLine>-Xmx2g</argLine> is an example. Keep any existing coverage-agent or debugging arguments. The 3.5.4 parameter reference limits this setting to forked execution. Repeat the failing test set and inspect its report and fork termination. If <forkCount>0</forkCount> is intentional, use branch 1 because the tests share Maven's JVM.

3. The exception belongs to forked javac. With Maven 3.9.9 and Compiler Plugin 3.14.0, set <maxmem> in megabytes in that plugin's configuration when <fork>true</fork> is in use. The 3.14.0 memory example shows the separate compiler settings. Repeat the failing compile phase and compare its outcome and memory use. Without a compiler fork, <maxmem> is not the heap control; return to branch 1.

Evidence and limits

Maven's release history dates 3.9.9 to 17 August 2024, and its release notes explicitly separate core and plugin releases. Pinning core alone does not select the Surefire or compiler version.

Jelena Bunčić's report, 29 February 2024, describes a Maven/Testcontainers build that still exhausts Java heap after a Docker service memory increase. It does not identify every JVM in the build.

The 2 GiB examples are starting budgets, not required sizes. Leave room for other JVMs, native allocations and containers; more concurrent forks multiply the total requirement. Repeated failure after a larger budget warrants inspection of retained objects or excessive inputs. Metaspace, native-thread failures and a bare operating-system kill require different diagnoses. Java 21 failure categories.

Started from a post by @Oracle, packaged by the editor. Original post

Subjects

More in documentation

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Maven: stop Java heap crashes during a build
Summary: For Maven 3.9.9 on Java 21, identify whether the heap failure belongs to Maven, a Surefire test fork or a forked compiler. Change that JVM’s limit rather than assuming one setting reaches every process. Maven’s own limit belongs in `MAVEN_OPTS`.
Copy for agent
get the whatworks thread maven-java-heap-space-oom-parent-test-compiler-jvm and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread maven-java-heap-space-oom-parent-test-compiler-jvm
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks