The nightly pipeline turns red at 02:14. The last lines of the console log read java.lang.OutOfMemoryError: Java heap space, and by 09:00 someone has already opened a ticket titled "Increase Jenkins memory". The controller's heap goes from 4 GB to 8 GB, the next night's build fails at the same step with the same message, and the ticket is reopened.

The rule everyone follows, raise -Xmx until the error goes away, assumes there is one JVM. In a typical Jenkins pipeline there are at least three, each with its own heap, and the message does not say which one died. Find the JVM first. The number comes second.

Three JVMs can print the same error

The Jenkins controller is a Java process. It schedules builds, stores pipeline state, renders the web interface and keeps build records in memory. The agent is another Java process, running the remoting library that receives commands from the controller. And the build itself, whenever it runs Maven, Gradle, sbt, a Surefire test fork or a Kotlin compiler daemon, starts one or more further JVMs inside the sh or bat step.

Each of them can throw OutOfMemoryError, and each prints it somewhere slightly different. The table is the quickest way to classify a failure.

Where the error appears Which JVM failed Typical trigger Setting that controls its heap
Console output, inside Maven or Gradle lines Build tool on the agent Large compile, annotation processing, big test suite MAVEN_OPTS, org.gradle.jvmargs
Console output, inside test runner output Test fork Tests loading large fixtures, leaks across tests Surefire argLine, Gradle maxHeapSize on test task
Controller log, web UI slow or 500 errors Jenkins controller Huge Groovy variables, many retained builds, a plugin JAVA_OPTS for the controller service
Agent goes offline, channel closed message Agent remoting JVM Rare: massive log streaming, archiving large files Agent launch JVM options

If the error appears in the build console between lines from mvn or gradle, the controller's heap is irrelevant. Raising it only takes memory away from other things on that host. If the controller log in /var/log/jenkins/jenkins.log or the systemd journal shows the error, and other jobs or the web interface stall at the same time, the controller is the patient.

A heap dump settles any doubt. Adding -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/tmp/dumps to the failing JVM writes an .hprof file at the moment of failure. Opening it in Eclipse Memory Analyzer and looking at the dominator tree usually names the culprit within minutes: a 1.2 GB String from a pipeline variable, millions of objects from one plugin, or a test fixture cache that never empties.

When the build tool runs out: Maven, Gradle and test forks

This is the most common case on modern Jenkins installations, because most heavy lifting happens on agents. Maven runs in the JVM started by the mvn script, which reads MAVEN_OPTS. Set it in the pipeline environment so the value is versioned with the code:

environment {
  MAVEN_OPTS = '-Xmx2g -XX:+HeapDumpOnOutOfMemoryError'
}

Maven also reads .mvn/jvm.config in the project root, which keeps the setting with the repository and applies on developer machines too. Tests are a separate matter. The Surefire and Failsafe plugins fork a new JVM for tests by default, and that fork ignores MAVEN_OPTS; its heap comes from the argLine parameter in the plugin configuration.

Gradle splits memory across the client, the daemon and worker processes. The daemon's heap is set through org.gradle.jvmargs=-Xmx3g in gradle.properties. Test JVMs take maxHeapSize on the test task, and Kotlin compilation may run in its own daemon with kotlin.daemon.jvmargs. Raising one of these does nothing for the others, which is why a Gradle build can keep failing after "the memory was increased".

Before raising anything, check the parallelism, since it is the setting people forget they changed. Gradle's org.gradle.parallel, Maven's -T 4 and Surefire's forkCount all multiply memory use. Four parallel test forks at 2 GB each need 8 GB plus the build tool itself. On an agent with 8 GB of RAM, that configuration fails no matter what the individual heaps are set to; it just fails as a container kill instead of a heap error. Sometimes the right fix is forkCount=2, not a bigger -Xmx.

When the controller runs out: Groovy variables, returnStdout and build history

Controller heap problems usually have one of three causes, and only one of them is fixed by a larger heap.

The first is pipeline code that pulls large data into Groovy. A scripted step such as def out = sh(script: 'cat build.log', returnStdout: true) copies the whole output into a string on the controller, because pipeline Groovy runs there, not on the agent.

A 300 MB log becomes at least 300 MB of heap, often more, and the continuation passing style engine may also serialise that variable into the build's program.dat file. readFile and readJSON on large files do the same. The fix is to keep big data on the agent: process it with shell tools or a script inside sh, and return only the small result.

The second is retention. Each kept build costs some memory when Jenkins loads its records, and a job with 5,000 retained builds of a matrix project can cost a surprising amount. Add buildDiscarder(logRotator(numToKeepStr: '50')) to the pipeline options and archive what you need elsewhere.

The third is a genuine need. A controller serving 200 active jobs with many plugins may simply need more than the default heap. On JDK 11 and later, the default maximum heap is a quarter of the memory the JVM sees, so a controller in a 4 GB container gets about 1 GB unless told otherwise. Set it explicitly. For a systemd install, systemctl edit jenkins and add Environment="JAVA_OPTS=-Xmx4g". For the official Docker image, pass JAVA_OPTS as an environment variable.

A controller that needs more heap every month has a leak, not a sizing problem. Plot heap after garbage collection over two weeks, using the Prometheus plugin or JMX. A flat sawtooth means the size is right. A rising floor means something is accumulating, and the heap dump will show what.

Containers change the arithmetic

On Kubernetes agents, the pod's memory limit and the JVM's heap are separate numbers that must fit together. If Maven's heap is 3 GB, Metaspace and thread stacks add several hundred megabytes, and the pod limit is 3 GB, the kernel kills the container with exit code 137 and Kubernetes reports OOMKilled.

The console shows a dead process, not a Java heap space message. That distinction matters: heap space means the JVM ran out of its own limit, while OOMKilled means the container ran out of the limit you set in the pod template.

Modern JDKs are container aware and size the heap from the cgroup limit. Using -XX:MaxRAMPercentage=75 instead of a fixed -Xmx lets the heap follow the pod limit, leaving a quarter for non heap memory. An illustrative sizing: a pod limit of 4 GiB with MaxRAMPercentage=75 gives a 3 GiB heap and about 1 GiB for Metaspace, code cache, threads and the shell that launched it.

Remember that a pod template can hold several containers. The jnlp container runs the agent remoting JVM; your maven or gradle container runs the build. Each has its own limit, and the build container is the one that needs room.

An illustrative case: the 02:14 failure

This composite case is illustrative. A team's nightly integration build, with 1,400 tests, started failing after a dependency upgrade. The first response raised the controller heap to 8 GB. Nothing changed, because the stack trace sat between Surefire lines in the console. The next attempt raised MAVEN_OPTS to 4 GB, which also changed nothing, because Surefire forks its own JVM.

A heap dump from the fork, enabled through argLine, showed 1.8 GB held by an in memory database cache that a newly upgraded test library no longer cleared between test classes. The fix was one line of configuration to reset the cache, plus an argLine of -Xmx1536m. The build went from failing at 47 minutes to passing in 41, and the controller heap went back to 4 GB.

Questions engineers ask about Jenkins heap errors

Is JavaScript heap out of memory in a Jenkins build the same problem? No. That message comes from Node.js, usually during a frontend build with webpack or a similar bundler. The fix is NODE_OPTIONS=--max-old-space-size=4096 or another value in megabytes, set in the pipeline environment, and Java settings have no effect on it.

Does restarting the Jenkins controller fix heap problems? It clears the heap, so the symptom disappears for a while. If the cause is a leak in a plugin or a growing set of retained builds, the error returns after a similar number of days, which is itself a useful diagnostic signal.

Should the Jenkins agent JVM get a large heap too? Rarely. The agent process mostly relays commands and streams logs, so a modest heap is enough for it. The memory that matters on an agent belongs to the build tools it launches, and those read their own settings.

What is a GC overhead limit exceeded error? It is a related OutOfMemoryError: the JVM was spending almost all its time in garbage collection while reclaiming very little memory. Treat it like a heap space error with an early warning, because the heap was nearly full for a while before it was thrown.

The pattern is moving toward smaller controllers and disposable agents. With Kubernetes pod agents, each build gets a fresh container sized by its own template, so build tool memory becomes a per pipeline setting reviewed alongside the code. The controller shrinks into a scheduler that should never hold build data at all. Teams that get there rarely see a controller heap error, and when a build JVM fails, the fix lives in one file in the repository.