Java的线程最大数量

时间:2021-12-26 10:35:42

转自: http://expectforever.spaces.live.com/

 出现以下错误:
Exception in thread "Thread-2" java.lang.OutOfMemoryError: unable to create new native thread
 at java.lang.Thread.start0(Native Method)
 at java.lang.Thread.start(Thread.java:574)
 at com.verye.nad.framework.communication.SocketListener.run(SocketListener.java:66)

发现:
JVM在启动一个线程的时候,将为线程分配堆栈,在jdk1.4上为每个线程分配256K,在jdk1.5上为每个线程分配1M,

每个JVM中的最大线程数 :max Thread = (2G-Xmx)/stacksize

JDK1.4
  -Xmx750 = 4580 threads.
  -Xmx1000 = 3608 threads.
  -Xmx1500M = 1663 threads

JDK1.5
  -Xmx750M = 1129 threads
  -Xmx1000M = 880 threads
  -Xmx1500M = 384 threads