Java heap space error in Tomcat, Eclipse, Maven and Gradle: which JVM failed, and where to set -Xmx

You raised -Xmx, restarted, and the build still dies with java.lang.OutOfMemoryError: Java heap space. Nine times out of ten the setting is fine; it just went to the wrong JVM. Servers, IDEs and build tools rarely run as a single Java process. Tomcat has a start-up JVM and a stop JVM, Eclipse runs your program in a separate process from the IDE, Maven forks test JVMs, and Gradle has a client, a daemon and workers. This guide shows, tool by tool, which process usually fails and the one place where its heap is set.
First, find the JVM that failed
Before changing any setting, identify the process. The stack trace and the log around it usually tell you:
- A trace that goes through your web application's classes, in the server log: the server JVM.
- A trace printed by the IDE itself, often in a dialog or the IDE's error log: the IDE's own JVM.
- A trace under a failing test, or a message saying a forked VM terminated: the test JVM forked by the build tool.
- A failure during compilation or dependency resolution, with no test involved: the build tool's own JVM (Maven) or the Gradle daemon.
When in doubt, list the running JVMs with jcmd (no arguments) while the job runs, then ask the suspect for its real settings with jcmd <pid> VM.flags. The MaxHeapSize it prints is the value that matters, whatever the configuration files say. Our guide to the Java heap space error then helps decide whether the heap is too small or whether something is leaking.
Tomcat: setenv and CATALINA_OPTS
Tomcat's own RUNNING.txt gives the rule. Apart from CATALINA_HOME and CATALINA_BASE, environment variables belong in a "setenv" script, placed in CATALINA_BASE/bin or CATALINA_HOME/bin and named setenv.sh on Unix-like systems or setenv.bat on Windows. The file is absent by default: you create it. The variable for heap settings is CATALINA_OPTS, which passes additional options to the java command that starts Tomcat. The same document warns not to use JAVA_OPTS for memory limits, because it is also applied to the small process that stops Tomcat.
# $CATALINA_BASE/bin/setenv.sh
CATALINA_OPTS="$CATALINA_OPTS -Xms1g -Xmx1g"
export CATALINA_OPTS
Three traps are common:
- Tomcat as a Windows service does not read
setenv.bat: the service has its own memory settings, set with the service manager. - Tomcat started by a package or by systemd may take its options from a unit file or a distribution-specific defaults file instead of
setenv.sh. Check the real command line withpsorjcmd <pid> VM.command_line. - Tomcat in a container: if you set a percentage of the container's memory rather than a fixed
-Xmx, read our page on the JVM in containers first.
Eclipse: the IDE and the programs it runs
Eclipse involves two different heaps. The first is the IDE's own JVM, configured in eclipse.ini, next to the Eclipse executable. The Eclipse wiki page on eclipse.ini explains that each option and each argument must be on its own line, and that all lines after -vmargs are passed as arguments to the JVM: heap settings therefore go at the end of the file, after -vmargs. If the IDE itself freezes or reports an out-of-memory error while indexing a large workspace, this is the file to edit, followed by a restart.
-vmargs
-Xms512m
-Xmx4g
The second heap belongs to the program you launch from Eclipse. Raising the IDE's heap does nothing for it. Each launch configuration has its own settings: in Run > Run Configurations, select the configuration, open the Arguments tab, which defines the arguments passed to the application and to the virtual machine, and add -Xmx2g in the VM arguments field (not in the program arguments, where it would be handed to your main method as a string). Tests launched as JUnit run configurations work the same way.
Maven: three different places
Maven's own JVM, the one that resolves dependencies and runs most plugins, is configured as described in the Configuring Apache Maven page. The MAVEN_OPTS environment variable contains the parameters used to start the JVM running Maven, for example -Xms256m -Xmx512m. A project can instead commit its options in .mvn/jvm.config at the top of the project, so every developer and every CI job gets the same settings. When an option is set in both places, Maven 3 uses the value from MAVEN_OPTS and Maven 4 the one from jvm.config.
# .mvn/jvm.config (committed with the project)
-Xmx2g
Tests are the usual surprise. Surefire runs tests in a forked JVM by default, and that JVM does not inherit MAVEN_OPTS. Its options come from the plugin's argLine parameter, which the Surefire documentation describes as arbitrary JVM options set on the command line, effective only for forked executions. Failsafe, for integration tests, has the same parameter.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>@{argLine} -Xmx1g</argLine>
</configuration>
</plugin>
The @{argLine} placeholder keeps options added by other plugins, such as a coverage agent, which a plain <argLine> value would overwrite.
Gradle: client, daemon, workers and tests
Gradle starts a lightweight client that hands the build to a long-lived daemon. According to Gradle's build environment guide, the org.gradle.jvmargs property sets the JVM arguments of the daemon, does not affect the client VM, and defaults to -Xmx512m "-XX:MaxMetaspaceSize=384m". It goes in gradle.properties, at the root of the project or in the Gradle user home. GRADLE_OPTS configures the client and is rarely what you want.
# gradle.properties
org.gradle.jvmargs=-Xmx2g -XX:MaxMetaspaceSize=512m
Test tasks fork their own JVMs again. The Test task reference exposes maxHeapSize, the maximum heap size for the process, and its example sets it to "512m":
tasks.withType<Test>().configureEach {
maxHeapSize = "1g"
}
Compilation can also fork, and Kotlin and Android builds start their own compiler daemons with their own settings. After changing org.gradle.jvmargs, stop the running daemons with gradle --stop, or the old daemon keeps serving builds with the old heap.
The global switches, and why to avoid them
Two environment variables reach almost every JVM a user starts. JDK_JAVA_OPTIONS, documented in the java command reference, is prepended by the java launcher to its command line; JAVA_TOOL_OPTIONS, defined by the JVM tool interface, is read by the virtual machine itself and also reaches processes started without the java launcher. Both work, and both are blunt: they give the same heap to the IDE, the build daemon, each test fork and the small command-line tools, and every JVM prints a notice that it picked them up. They are useful for a quick experiment, not as a permanent fix.
A short checklist
- Read the stack trace and find which process failed.
- Check its real heap with
jcmd <pid> VM.flags. - Set
-Xmxin the one place that reaches that process:setenv.shfor Tomcat,eclipse.inior the launch configuration in Eclipse,.mvn/jvm.configorargLinefor Maven,org.gradle.jvmargsormaxHeapSizefor Gradle. - Restart the right thing: Tomcat, the IDE, or the Gradle daemon.
- If the error comes back with a bigger heap, stop raising it and look for a leak, starting with a heap dump.
For what -Xmx actually limits, and what it leaves out, see the Java heap explained.
MemoryJava heap space
The most common OutOfMemoryError has three different causes. How to tell an undersized heap, a leak and one oversized request apart.
MemoryThe Java heap explained
Eden, survivors, old generation, G1 regions: what lives where in the Java heap, why most objects die young, and why -Xmx is only one line of a JVM's memory bill.
ProductionThe JVM in containers
How limits are detected, what MaxRAMPercentage really sets, and a budget that keeps the OOM killer away.