Java: What is the limit to the number of threads you can create?

Overview

I have seen a number of tests where a JVM has 10K threads. However, what happens if you go beyond this?

My recommendation is to consider having more servers once your total reaches 10K. You can get a decent server for $2K and a powerful one for $10K.

Creating threads gets slower

The time it takes to create a thread increases as you create more thread. For the 32-bit JVM, the stack size appears to limit the number of threads you can create. This may be due to the limited address space. In any case, the memory used by each thread's stack add up. If you have a stack of 128KB and you have 20K threads it will use 2.5 GB of virtual memory.

BitnessStack SizeMax threads
32-bit 64K32,073
32-bit128K20,549
32-bit256K11,216
64-bit 64Kstack too small
64-bit128K32,072
64-bit512K32,072
Note: in the last case, the thread stacks total 16 GB of virtual memory.

Java 6 update 26 32-bit,-XX:ThreadStackSize=64
4,000 threads: Time to create 4,000 threads was 0.522 seconds 
8,000 threads: Time to create 4,000 threads was 1.281 seconds 
12,000 threads: Time to create 4,000 threads was 1.874 seconds 
16,000 threads: Time to create 4,000 threads was 2.725 seconds 
20,000 threads: Time to create 4,000 threads was 3.333 seconds 
24,000 threads: Time to create 4,000 threads was 4.151 seconds 
28,000 threads: Time to create 4,000 threads was 5.293 seconds 
32,000 threads: Time to create 4,000 threads was 6.636 seconds 
After creating 32,073 threads, java.lang.OutOfMemoryError: unable to create new native thread
 at java.lang.Thread.start0(Native Method)
 at java.lang.Thread.start(Thread.java:640)
 at com.google.code.java.core.threads.MaxThreadsMain.addThread(MaxThreadsMain.java:46)
 at com.google.code.java.core.threads.MaxThreadsMain.main(MaxThreadsMain.java:16)

Java 6 update 26 32-bit,-XX:ThreadStackSize=128
4,000 threads: Time to create 4,000 threads was 0.525 seconds 
8,000 threads: Time to create 4,000 threads was 1.239 seconds 
12,000 threads: Time to create 4,000 threads was 1.902 seconds 
16,000 threads: Time to create 4,000 threads was 2.529 seconds 
20,000 threads: Time to create 4,000 threads was 3.165 seconds 
After creating 20,549 threads, java.lang.OutOfMemoryError: unable to create new native thread
 at java.lang.Thread.start0(Native Method)
 at java.lang.Thread.start(Thread.java:640)
 at com.google.code.java.core.threads.MaxThreadsMain.addThread(MaxThreadsMain.java:46)
 at com.google.code.java.core.threads.MaxThreadsMain.main(MaxThreadsMain.java:16)

Java 6 update 26 32-bit,-XX:ThreadStackSize=128
4,000 threads: Time to create 4,000 threads was 0.526 seconds 
8,000 threads: Time to create 4,000 threads was 1.212 seconds 
After creating 11,216 threads, java.lang.OutOfMemoryError: unable to create new native thread
 at java.lang.Thread.start0(Native Method)
 at java.lang.Thread.start(Thread.java:640)
 at com.google.code.java.core.threads.MaxThreadsMain.addThread(MaxThreadsMain.java:46)
 at com.google.code.java.core.threads.MaxThreadsMain.main(MaxThreadsMain.java:16)

Java 6 update 26 64-bit,-XX:ThreadStackSize=128
4,000 threads: Time to create 4,000 threads was 0.577 seconds 
8,000 threads: Time to create 4,000 threads was 1.292 seconds 
12,000 threads: Time to create 4,000 threads was 1.995 seconds 
16,000 threads: Time to create 4,000 threads was 2.653 seconds 
20,000 threads: Time to create 4,000 threads was 3.456 seconds 
24,000 threads: Time to create 4,000 threads was 4.663 seconds 
28,000 threads: Time to create 4,000 threads was 5.818 seconds 
32,000 threads: Time to create 4,000 threads was 6.792 seconds 
After creating 32,072 threads, java.lang.OutOfMemoryError: unable to create new native thread
 at java.lang.Thread.start0(Native Method)
 at java.lang.Thread.start(Thread.java:640)
 at com.google.code.java.core.threads.MaxThreadsMain.addThread(MaxThreadsMain.java:46)
 at com.google.code.java.core.threads.MaxThreadsMain.main(MaxThreadsMain.java:16)


Java 6 update 26 64-bit,-XX:ThreadStackSize=512
4,000 threads: Time to create 4,000 threads was 0.577 seconds 
8,000 threads: Time to create 4,000 threads was 1.292 seconds 
12,000 threads: Time to create 4,000 threads was 1.995 seconds 
16,000 threads: Time to create 4,000 threads was 2.653 seconds 
20,000 threads: Time to create 4,000 threads was 3.456 seconds 
24,000 threads: Time to create 4,000 threads was 4.663 seconds 
28,000 threads: Time to create 4,000 threads was 5.818 seconds 
32,000 threads: Time to create 4,000 threads was 6.792 seconds 
After creating 32,072 threads, java.lang.OutOfMemoryError: unable to create new native thread
 at java.lang.Thread.start0(Native Method)
 at java.lang.Thread.start(Thread.java:640)
 at com.google.code.java.core.threads.MaxThreadsMain.addThread(MaxThreadsMain.java:46)
 at com.google.code.java.core.threads.MaxThreadsMain.main(MaxThreadsMain.java:16)

The Code

MaxThreadsMain.java

Comments

Popular posts from this blog

Java is Very Fast, If You Don’t Create Many Objects

System wide unique nanosecond timestamps

Comparing Approaches to Durability in Low Latency Messaging Queues