ThreadStackSize -Xss

/*
 * Copyright (c) 2011.  Peter Lawrey
 *
 * "THE BEER-WARE LICENSE" (Revision 128)
 * As long as you retain this notice you can do whatever you want with this stuff.
 * If we meet some day, and you think this stuff is worth it, you can buy me a beer in return
 * There is no warranty.
 */

package com.google.code.java.core.threads;

import java.util.ArrayList;
import java.util.List;

public class MaxThreadsMain {

  public static final int BATCH_SIZE = 4000;

  public static void main(String... args) throws InterruptedException {
    List<Thread> threads = new ArrayList<Thread>();
    try {
      for (int i = 0; i <= 100 * 1000; i += BATCH_SIZE) {
        long start = System.currentTimeMillis();
        addThread(threads, BATCH_SIZE);
        long end = System.currentTimeMillis();
        Thread.sleep(1000);
        long delay = end - start;
        System.out.printf("%,d threads: Time to create %,d threads was %.3f seconds %n", threads.size(), BATCH_SIZE, delay / 1e3);
      }
    } catch (Throwable e) {
      System.err.printf("After creating %,d threads, ", threads.size());
      e.printStackTrace();
    }

  }

  private static void addThread(List<Thread> threads, int num) {
    for (int i = 0; i < num; i++) {
      Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
          try {
            while (!Thread.interrupted()) {
              Thread.sleep(1000);
            }
          } catch (InterruptedException ignored) {
            //
          }
        }
      });
      t.setDaemon(true);
      t.setPriority(Thread.MIN_PRIORITY);
      threads.add(t);
      t.start();
    }
  }
}



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. 

Bitness Stack Size Max threads
32-bit  64K 32,073
32-bit 128K 20,549
32-bit 256K 11,216
64-bit  64K stack too small
64-bit 128K 32,072
64-bit 512K 32,072
Note: in the last case, the thread stacks total 16 GB of virtual memory.

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

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

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

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


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

The Code

MaxThreadsMain.java

转载于:https://my.oschina.net/u/138995/blog/213495

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值