613 threads limit on debian [closed]

When running this program thread-limit.c on my dedicated debian server, the output says that my system can’t create more than around 600 threads. I need to create more threads, and fix my system misconfiguration.

Here are a few informations about my dedicated server:

de801:/# uname -a
Linux de801.ispfr.net 2.6.18-028stab085.5 #1 SMP Thu Apr 14 15:06:33 MSD 2011 x86_64 GNU/Linux
de801:/# java -version
java version "1.6.0_26"
Java(TM) SE Runtime Environment (build 1.6.0_26-b03)
Java HotSpot(TM) 64-Bit Server VM (build 20.1-b02, mixed mode)
de801:/# ldd $(which java)
        linux-vdso.so.1 =>  (0x00007fffbc3fd000)
        libpthread.so.0 => /lib/libpthread.so.0 (0x00002af013225000)
        libjli.so => /usr/lib/jvm/java-6-sun-1.6.0.26/jre/bin/../lib/amd64/jli/libjli.so (0x00002af013441000)
        libdl.so.2 => /lib/libdl.so.2 (0x00002af01354b000)
        libc.so.6 => /lib/libc.so.6 (0x00002af013750000)
        /lib64/ld-linux-x86-64.so.2 (0x00002af013008000)
de801:/# cat /proc/sys/kernel/threads-max
1589248
de801:/# 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) 794624
max locked memory       (kbytes, -l) 32
max memory size         (kbytes, -m) unlimited
open files                      (-n) 10240
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 128
cpu time               (seconds, -t) unlimited
max user processes              (-u) unlimited
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

Here is the output of the C program

de801:/test# ./thread-limit
Creating threads ...
Address of c = 1061520 KB
Address of c = 1081300 KB
Address of c = 1080904 KB
Address of c = 1081168 KB
Address of c = 1080508 KB
Address of c = 1080640 KB
Address of c = 1081432 KB
Address of c = 1081036 KB
Address of c = 1080772 KB
100 threads so far ...
200 threads so far ...
300 threads so far ...
400 threads so far ...
500 threads so far ...
600 threads so far ...
Failed with return code 12 creating thread 637.

Any ideas how to fix this please ?

Answer

You don’t need to create more than 600 threads at a time. You only need threads for things you can usefully do at the same time. Your system cannot usefully do more than 600 things at a time. You do not need more threads to do more work.

In any event, you’re setting the wrong stack size. You’re setting the limiting size of the initial thread stack, the one not created by your code. You’re supposed to be setting the size of the stack of the threads your code creates.

Change:

  printf("Creating threads ...\n");
  for (i = 0; i < MAX_THREADS && rc == 0; i++) {
    rc = pthread_create(&(thread[i]), NULL, (void *) &run, NULL);

to (roughly):

  printf("Creating threads ...\n");
  pthread_attr_t pa;
  pthread_attr_init(&pa);
  pthread_attr_setstacksize(&pa, 2*1024*1024);
  for (i = 0; i < MAX_THREADS && rc == 0; i++) {
    rc = pthread_create(&(thread[i]), &pa, (void *) &run, NULL);
  phtread_attr_destroy(&pa);

You can also call pthread_attr_getstacksize to find our your platform’s default thread stack size. Note that if a thread exceeds its allocated stack, your process will die.

Even with a 2MB stack, which can be very dangerous, you’re still looking at 1.2GB just for stacks to have 600 threads. Whatever your job is, threads are not the right tool for it.

Attribution
Source : Link , Question Author : Joel , Answer Author : David Schwartz

Leave a Comment