Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OS X Java maximum threads per process

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?

like image 206
mixel Avatar asked Jun 13 '13 09:06

mixel


People also ask

How many threads can a Java process have?

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.

How many threads can a process handle?

A process can have anywhere from just one thread to many threads.

How many threads can concurrently Java?

concurrent package is 16.

What is the maximum number of running threads per application instance?

The number of running threads per application instance is limited to 10 420. Reaching this limit can cause performance issues.


1 Answers

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.

like image 70
fge Avatar answered Sep 30 '22 08:09

fge