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)

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.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →