Java "unable to create new native thread" During Build in CI
The JVM could not create another OS thread. This is native-memory or limit exhaustion, not heap - the process hit the thread ulimit, the cgroup pids limit, or ran out of address space for thread stacks.
What this error means
A build with high parallelism fails with java.lang.OutOfMemoryError: unable to create new native thread. The heap may be far from full; the failure is in native thread creation.
java.lang.OutOfMemoryError: unable to create new native thread
at java.base/java.lang.Thread.start0(Native Method)
at java.base/java.lang.Thread.start(Thread.java:809)Common causes
Thread/pids limit reached
The OS ulimit -u or a container pids limit caps how many threads the process can spawn. High build/test parallelism hits that cap.
Native memory exhausted by thread stacks
Each thread reserves stack space outside the heap. A huge heap plus many threads leaves too little native memory/address space to create more.
How to fix it
Reduce concurrency
Lower worker and fork counts so fewer threads are created at once.
# Gradle: gradle.properties
org.gradle.workers.max=2
# test { maxParallelForks = 1 }
# Maven surefire: <forkCount>1</forkCount>Raise OS/container limits
Increase the thread/pids limit for the build process where you control the runner.
ulimit -u 8192
# container: run with a higher pids limit (e.g. --pids-limit)How to prevent it
- Bound build/test parallelism, keep heap modest so native memory remains for thread stacks, and provision adequate thread/pids limits on runners.