Running this code
for (int i = 0; i < 4000; i++) {
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(60000);
} catch (InterruptedException e) {
}
}
}).start();
System.out.println(i);
}
results in
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:658)
at com.codeoverdrive.burnbearburn.Main.main(Main.java:10)
after 2024 running threads. Playing with JVM heap and stack sizes does not help.
sysctl kern.num_threads
returns
kern.num_threads: 10240
OS X Mountain Lion 10.8.4, Macbook Air with 4GB RAM
Any suggestions?