Java "unable to create new native thread" During Build in CI
By Daniel Zoghalchali·Latchkey
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 runtime
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.
Increase the thread/pids limit for the build process where you control the runner.
Terminal
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.
Frequently asked questions
What causes ""unable to create new native thread""?
The OS ulimit -u or a container pids limit caps how many threads the process can spawn. High build/test parallelism hits that cap.
How do I fix "unable to create new native thread"?
Lower worker and fork counts so fewer threads are created at once.
Can Latchkey fix this automatically?
Yes. Latchkey runs your GitHub Actions on managed runners that detect this failure, apply the fix, and retry the job automatically - self-healing is on by default.