新建一个类,导入如下的测试代码:
public class TestNativeOutOfMemoryError {
public static void main(String[] args) { for (int i = 0;; i++) {
System.out.println("i = " + i);
new Thread(new HoldThread()).start();
}
} } class HoldThread extends Thread {
CountDownLatch cdl = new CountDownLatch(1); public HoldThread() {
this.setDaemon(true);
} public void run() {
try {
cdl.await();
} catch (InterruptedException e) {
}
}
}
在我的开发环境上运行结果如下:
......
i = 3935
Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread
at java.lang.Thread.start0(Native Method)
at java.lang.Thread.start(Thread.java:714)
at com.study.thinking.in.java.concurrent.TestNativeOutOfMemoryError.main(TestNativeOutOfMemoryError.java:15)
每次运行结果并不完全一致,但是大差不差。
影响结果的几个参数:
1. 物理内存
使用64位OS能给虚拟机分配更大内存
2.JVMMemory
减少这项参数能增加并发的线程数,非倍数关系
-Xms1024m -Xmx1024m并不能比-Xms512m -Xmx512m多一倍的线程
3.ThreadStackSize
减少单个线程的栈大小,在有效范围内,是倍数关系
更多消息参考:http://sesame.iteye.com/blog/622670