Skip to content
Latchkey

Java "unable to create new native thread" / OOM Thread in CI

The JVM could not create another OS thread. Despite the OutOfMemoryError name, this is not Java heap - it is the OS refusing more threads (ulimit/pid cap) or no native memory left for thread stacks.

What this error means

The application or test framework fails with java.lang.OutOfMemoryError: unable to create new native thread, typically after spawning many threads (thread pools, parallel tests, connection pools).

JVM output
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)

Diagnose it: JDK version and heap

Terminal
java -version 2>&1
echo "JAVA_HOME=$JAVA_HOME"
free -h

# class file 65 = Java 21, 61 = Java 17, 55 = Java 11
javap -verbose <SomeClass>.class | grep "major version"

Common causes

OS thread/process limit reached

The container or user hit ulimit -u (max processes/threads) or a cgroup pids limit. The OS denies new threads regardless of free RAM.

Native memory exhausted by thread stacks

Each thread reserves stack memory (default ~1 MB). Thousands of threads, or a large -Xss, exhaust native memory so no more can be created.

How to fix it

Raise the OS thread/process limit

Increase the ulimit (and cgroup pids limit if containerized) so the OS allows more threads.

Terminal
ulimit -u            # current limit
ulimit -u 8192       # raise for this shell/job
# container: set pids limit appropriately (e.g. docker --pids-limit)

Reduce thread usage and stack size

  1. Bound thread pools instead of unbounded newCachedThreadPool growth.
  2. Lower test parallelism so fewer threads run concurrently.
  3. Reduce per-thread stack with -Xss512k if many threads are genuinely needed.

How to prevent it

  • Use bounded thread pools sized to the workload.
  • Set adequate ulimit -u/cgroup pids limits on runners.
  • Cap test/build parallelism so thread counts stay reasonable.

Frequently asked questions

What causes Java "unable to create new native thread" / OOM thread in CI?
There are 2 common causes: os thread/process limit reached and native memory exhausted by thread stacks. The container or user hit ulimit -u (max processes/threads) or a cgroup pids limit.
How do I fix Java "unable to create new native thread" / OOM thread in CI?
There are 2 fixes depending on which cause you have: raise the os thread/process limit and reduce thread usage and stack size. Work through them in order, since the first is the most common.
What does Java "unable to create new native thread" / OOM thread in CI actually mean?
The application or test framework fails with java.lang.OutOfMemoryError: unable to create new native thread, typically after spawning many threads (thread pools, parallel tests, connection pools).
How do I stop Java "unable to create new native thread" / OOM thread in CI happening again?
Use bounded thread pools sized to the workload. The prevention section lists 3 changes that keep it from recurring.
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.

Related guides

References

This is an out-of-memory kill, not an application error. Latchkey detects, repairs, and retries it for you. Start free → 30-day trial · No credit card