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?
Each JVM server can have a maximum of 256 threads to run Java applications. In a CICS region you can have a maximum of 2000 threads. If you have many JVM servers running in the CICS region (for example, more than seven), you cannot set the maximum value for every JVM server.
A process can have anywhere from just one thread to many threads.
concurrent package is 16.
The number of running threads per application instance is limited to 10 420. Reaching this limit can cause performance issues.
The most probable cause is that the user you are running with has a limit on the number of threads it can create.
Try and increase this limit. The current limit can be shown by the ulimit -u
command. Note that as a regular user you probably won't be able to increase it, you'll have to modify the environment as root.
And yes, OutOfMemoryError
is misleading in this case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With