java 线程栈空间_java线程的内存不包含在JVM堆与栈中
Java代碼 ?
publicclassMaxThreadsTest?{
publicstaticvoidmain(String[]?args)?{
while(true)?{
newThread(newA()).start();
if(A.eflag.get()){
return;
}
}
}
}
classAimplementsRunnable?{
staticAtomicInteger?counts?=newAtomicInteger(0);
staticAtomicBoolean?eflag?=newAtomicBoolean(false);
@Override
publicvoidrun()?{
try{
intx?=?counts.getAndIncrement();
if(x%200==0)
System.out.println("conts:?"+?A.counts);
Thread.sleep(30000);
}catch(InterruptedException?e)?{
eflag.set(true);
e.printStackTrace();
}
}
}
public class MaxThreadsTest {
public static void main(String[] args) {
while (true) {
new Thread(new A()).start();
if(A.eflag.get()){
return;
}
}
}
}
class A implements Runnable {
static AtomicInteger counts = new AtomicInteger(0);
static AtomicBoolean eflag = new AtomicBoolean(false);
@Override
public void run() {
try {
int x = counts.getAndIncrement();
if(x 0==0)
System.out.println("conts: " + A.counts);
Thread.sleep(30000);
} catch (InterruptedException e) {
eflag.set(true);
e.printStackTrace();
}
}
}
Java代碼 ?
-Xms16M?-Xmx32M?-Xss1M?-XX:+HeapDumpOnOutOfMemoryError
conts:1801
Exception?in?thread"main"java.lang.OutOfMemoryError:?unable?to?createnewnativethread
-Xms32M?-Xmx64M?-Xss1M?-XX:+HeapDumpOnOutOfMemoryError
conts:1801
Exception?in?thread"main"java.lang.OutOfMemoryError:?unable?to?createnewnativethread
//把Xmx設大可以建立的線程數反而變小,這是個很奇怪的事情:
-Xms64M?-Xmx128M?-Xss1M?-XX:+HeapDumpOnOutOfMemoryError
conts:1601
Exception?in?thread"main"java.lang.OutOfMemoryError:?unable?to?createnewnativethread
//把Xmx設大可以建立的線程數反而變小,這是個很奇怪的事情:
-Xms128M?-Xmx256M?-Xss1M?-XX:+HeapDumpOnOutOfMemoryError
conts:1601
Exception?in?thread"main"java.lang.OutOfMemoryError:?unable?to?createnewnativethread
//把Xss設小有驚人效果:
-Xms16M?-Xmx32M?-Xss128K?-XX:+HeapDumpOnOutOfMemoryError
conts:13401
Exception?in?thread"main"java.lang.OutOfMemoryError:?unable?to?createnewnativethread
-Xms16M -Xmx32M -Xss1M -XX:+HeapDumpOnOutOfMemoryError
conts: 1801
Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread
-Xms32M -Xmx64M -Xss1M -XX:+HeapDumpOnOutOfMemoryError
conts: 1801
Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread
//把Xmx設大可以建立的線程數反而變小,這是個很奇怪的事情:
-Xms64M -Xmx128M -Xss1M -XX:+HeapDumpOnOutOfMemoryError
conts: 1601
Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread
//把Xmx設大可以建立的線程數反而變小,這是個很奇怪的事情:
-Xms128M -Xmx256M -Xss1M -XX:+HeapDumpOnOutOfMemoryError
conts: 1601
Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread
//把Xss設小有驚人效果:
-Xms16M -Xmx32M -Xss128K -XX:+HeapDumpOnOutOfMemoryError
conts: 13401
Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread
以上是在eclipse里測試的。
在windows下直接測試有明顯不同。
java MaxThreadsTest -Xms16M -Xmx32M -Xss1M
java MaxThreadsTest -Xms64M -Xmx128M -Xss1M
無論怎么設置都在5401后結束。
我對JVM的了解基本為0,所以也分析不出原因。
請大家幫我看看,這是怎么回事,不是說Xmx越大、Xss越小,則線程數越多么?
被移到問答區里了,這么低的懸賞,能有人關注嗎?
沒辦法了,就這么點錢啦……………………………………………………………………
2010-12-17 flootball (高級程序員)
你的操作系統的內存是多少?
在java中每new一個線程,jvm都是向操作系統請求new一個本地線程,此時操作系統會使用剩余的內存空間來為線程分配內存,而不是使用jvm的內存。這樣,當操作系統的可用內存越少,則jvm可用創建的新線程也就越少。
總結
以上是生活随笔為你收集整理的java 线程栈空间_java线程的内存不包含在JVM堆与栈中的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 深度拷贝 java_Java深度拷贝方式
- 下一篇: java dispatchevent_d