sbt "java.lang.OutOfMemoryError: Metaspace" in CI
The JVM hosting sbt ran out of Metaspace, the region that holds class metadata. Scala compilation loads many classes, and a capped MaxMetaspaceSize or an accumulating sbt session overflows it. Raising the limit through SBT_OPTS fixes it.
What this error means
sbt fails mid-compile with "java.lang.OutOfMemoryError: Metaspace" and the build may abort or the sbt process die.
[error] java.lang.OutOfMemoryError: Metaspace
[error] Use 'last' for the full log.
[error] (Compile / compileIncremental) java.lang.OutOfMemoryError: MetaspaceCommon causes
MaxMetaspaceSize capped too low
An explicit -XX:MaxMetaspaceSize in SBT_OPTS or the launcher is smaller than a large multi-module Scala compile needs.
Long-lived sbt session accumulating classes
A persistent sbt session that reloads and compiles many times keeps loading classes until Metaspace fills.
How to fix it
Raise Metaspace via SBT_OPTS
- Set a larger
-XX:MaxMetaspaceSizeinSBT_OPTSfor the CI step. - Run sbt so the launcher picks up the bigger limit.
- Confirm the compile completes without the OOM.
env:
SBT_OPTS: "-Xmx2g -XX:MaxMetaspaceSize=1g"Run sbt in batch mode so the JVM exits
Batch each job so the sbt JVM starts fresh and releases Metaspace instead of accumulating across many interactive commands.
sbt --batch clean compile testHow to prevent it
- Set an adequate
-XX:MaxMetaspaceSizeinSBT_OPTSfor large builds. - Run one batched sbt invocation per job so the JVM does not accumulate classes.
- Provision runners with enough memory for the compile.