3

I have the following env:

$ ulimit -s
100000
$ ulimit -i
63645
$ cat /proc/sys/kernel/threads-max 
127626
$ cat /proc/sys/vm/max_map_count 
600000
$ cat /proc/sys/kernel/pid_max 
200000
$ java -Xmx4G -Xss256k -cp . ThreadCreation
...
11542
11543
java.lang.OutOfMemoryError: unable to create new native thread
    at java.lang.Thread.start0(Native Method)
    at java.lang.Thread.start(Thread.java:717)
    at ThreadCreation.main(ThreadCreation.java:15)

The following testing class

public class ThreadCreation {
    public static void main(String[] args) {
        try {
            for (int i = 0; i < 100000; i++) {
                System.out.println(i);
                new Thread("Thread-" + i) {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(1000000000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
            }

        } catch (Throwable t) {
            t.printStackTrace();
            System.exit(0);
        }
    }
}

This is the ulimit status:

$ ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 63645
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 100000
cpu time               (seconds, -t) unlimited
max user processes              (-u) 63645
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

Can you please help me on what I'm doing wrong? Thanks! It's OpenJDK 1.8.0. I know I should use ExecutorService, but this is just for testing/demo purposes.

There should be enough memory:

$ free
              total        used        free      shared  buff/cache   available
Mem:       16336132     5935372     3513992     1377844     6886768     8600916
Swap:      16678908           0    16678908

I tried lots of suggestions suggested by other StackOverflow questions but none worked for me; that's why I'm opening a new question.

Martin Vysny
  • 3,088
  • 28
  • 39
  • Do some logging in `run` - I guess not all threads will actually be run, a limited number of threads will be used to swap between all started Threads. – Joop Eggen Nov 02 '17 at 08:28

1 Answers1

0

Ah, found the answer here: How to increase maximum number of JVM threads (Linux 64bit)

The culprit was systemd which capped the number of processes at around 12288; increasing UserTasksMax helped me to break the 12k limit.

Martin Vysny
  • 3,088
  • 28
  • 39