The ThreadLimits project tries to empirically determine how many threads can be created in a JVM with a given memory and CPU limit. The goal is to prove that CPU has no impact on the amount of threads that can be created, while memory has a major impact.
The project create a new thread until it fails with OOM exception or JVM native one. Counter for Every new thread is displayed in the console. E.g. last line with:
New thread #6804.
means that there were 6804 theads created before the JVM failed.
Use JDK 22 to build the project.
E.g. with sdkman:
sdk use java 22-zulu
mvn clean package
docker build -t threadlimits .
Basic run with no limits:
docker run threadlimits
Below are my results for three runs of the same docker command with memory/CPU limits added. You may notice that for the same limit params I've got more or less similar results.
docker run --memory=256m --cpus="1.0" threadlimits
- New thread #6804.
- New thread #6812.
- New thread #5825.
docker run --memory=256m --cpus="8.0" threadlimits
- New thread #6637
- New thread #7575
- New thread #7588
docker run --memory=512m --cpus="1.0" threadlimits
- New thread #12907
- New thread #12704
- New thread #12820
docker run --memory=512m --cpus="8.0" threadlimits
- New thread #13643
- New thread #12676
- New thread #13468
docker run --memory=1024m --cpus="1.0" threadlimits
- New thread #26186
- New thread #25297
- New thread #25521
docker run --memory=1024m --cpus="8.0" threadlimits
- New thread #26871
- New thread #26699
- New thread #26185
Although the results may vary depending on the machine you're running the tests on, you should be able to see the overall trend. Rising the memory for the JVM allows for more threads to be created, while CPU has no visible impact on that amount.