Questo test dimostra il numero massimo di thread che può essere creato in JavaPerché il numero massimo di thread diminuisce quando aumento la dimensione massima dell'heap?
System.out.println("Max memory " + Runtime.getRuntime().maxMemory()/1024/1024 + "M");
for (int i = 0;; i++) {
Thread t = new Thread() {
public void run() {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
}
};
};
try {
t.start();
} catch (Error e) {
System.out.println("Max threads " + i);
e.printStackTrace();
System.exit(1);
}
}
Quando eseguo con dimensione predefinita heap (256M) ottengo
Max memory 247M
Max threads 2247
java.lang.OutOfMemoryError: unable to create new native thread
at java.lang.Thread.start0(Native Method)
at java.lang.Thread.start(Thread.java:691)
at test.Test1.main(Test1.java:19)
quando ho aumentare dimensione heap massima di 512M ottengo
Max memory 494M
Max threads 1906
...
quando ho aumentare la dimensione massima heap 1024M ricevo
Max memory 989M
Max threads 1162
...
Cioè, più memoria heap meno thread. Perché?
Potremmo vedere il tuo 'java -version'? – NPE
È Client HotSpot (TM) VM 1.7.0_21 su Windows 7 –
Ulteriori thread = meno heap –