Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 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) =>  (0x00007fffbc3fd000) => /lib/ (0x00002af013225000) => /usr/lib/jvm/java-6-sun- (0x00002af013441000) => /lib/ (0x00002af01354b000) => /lib/ (0x00002af013750000)
        /lib64/ (0x00002af013008000)
de801:/# cat /proc/sys/kernel/threads-max
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 ?

share|improve this question

closed as off topic by Zoredache, mdpc, Iain, Mark Henderson Nov 25 '11 at 2:58

Questions on Server Fault are expected to relate to server, networking, or related infrastructure administration within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

Why do you need so many? Anyway, see this question.… – Zoredache Nov 21 '11 at 22:30
thank you, i have already seen each of those links and they don't apply to my problem. none of the solutions proposed changes anything. – Joel Nov 21 '11 at 22:44
Is there actually enough allocatable memory on the system to generate more threads? Do you have the results of free? – Matthew Ife Nov 22 '11 at 0:12
Over on SuperUser, where this questioner has also spammed this very same programming question, I pointed out that the question has been asked and answered more than seven times on StackOverflow, including twice in August 2010 alone. – JdeBP Nov 24 '11 at 22:57

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.


  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_setstacksize(&pa, 2*1024*1024);
  for (i = 0; i < MAX_THREADS && rc == 0; i++) {
    rc = pthread_create(&(thread[i]), &pa, (void *) &run, NULL);

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.

share|improve this answer
I executed this code on my server and got the following result: Failed with return code 12 creating thread 640.. I also changed the stack size with pthread_attr_setstacksize(&pa, 128*1024); and got the following result: Failed with return code 12 creating thread 635.. – Joel Nov 21 '11 at 23:56
I forgot to mention that this is not my code but a standard linux test that should create 20.000+ threads on any linux. – Joel Nov 21 '11 at 23:58
@Joel Can you paste a link to the code as you tested it? If you, for example, missed the change to pthread_create, the code won't work as intended. – David Schwartz Nov 22 '11 at 0:02
there it it: thread-limit3.c – Joel Nov 22 '11 at 0:04
@Joel On 64-bit Linux, I get up to 10,000 threads with that code, after raising the 'ulimit -u' value to unlimited. – David Schwartz Nov 22 '11 at 0:09

This is nothing to do with thread limits. Error code 12 doesnt apply to thread limits.

$ perror 12
OS error code  12:  Cannot allocate memory

I guess if anything your running low on stack, if you set it to the redhat default of 8MB your probably going to be able to run more threads.

This one is the same as the one linked to with explicit stack sizes set to 8MB. See what happens when you compile this one.

/* compile with:   gcc -lpthread -o thread-limit thread-limit.c */
/* originally from: */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/time.h>
#include <sys/resource.h>

#define MAX_THREADS 10000
int i;

void run(void) {
  char c;
  if (i < 10)
    printf("Address of c = %u KB\n", (unsigned int) &c / 1024);
  sleep(60 * 60);

int main(int argc, char *argv[]) {
  struct rlimit limits;
  limits.rlim_cur = 1024*1024*8;
  limits.rlim_max = 1024*1024*8;
  if (setrlimit(RLIMIT_STACK, &limits) == -1) {
    perror("Failed to set stack limit");
  int rc = 0;
  pthread_t thread[MAX_THREADS];
  printf("Creating threads ...\n");
  for (i = 0; i < MAX_THREADS && rc == 0; i++) {
    rc = pthread_create(&(thread[i]), NULL, (void *) &run, NULL);
    if (rc == 0) {
      if ((i + 1) % 100 == 0)
    printf("%i threads so far ...\n", i + 1);
      printf("Failed with return code %i creating thread %i.\n",
         rc, i + 1);
share|improve this answer
i have tried every possible value for the stack size and the limit is always the same. – Joel Nov 21 '11 at 23:12
@Joel I altered the provided C to set a stack limit. Since your stack size is pretty small I'm quietly confident that is your issue. Might need to use root to set that limit correctly. – Matthew Ife Nov 21 '11 at 23:28
This code sets an 8MB limit on the process initial (legacy) stack. It has no effect on the stacks created for each individual thread -- at the process level, these are data. – David Schwartz Nov 21 '11 at 23:38
Thanks for the answer. I am already a root on this machine. And running the code you provided returns the following result: Failed with return code 12 creating thread 637.. – Joel Nov 21 '11 at 23:43
@David Schwartz. According to the man page "A child process created via fork(2) inherits its parent's resource limits. Resource limits are preserved across execve(2).", an explicit getrlimit in run() proves this is also the case for threads not yet created too. – Matthew Ife Nov 21 '11 at 23:46
up vote -1 down vote accepted

Just got the following information: This is a limitation imposed by my host provider. This has nothing to do with programming, or linux.

share|improve this answer
So either they're being completely unreasonable, prohibiting a perfectly sensible program from running, or you're being completely unreasonable, claiming that your design is perfectly sensible. – David Schwartz Nov 22 '11 at 17:23
yeah whatever you say – Joel Nov 22 '11 at 17:26
On the off chance you are open to reason, please at least take a quick glance at the actual standard you are coding to which says "_POSIX_THREAD_THREADS_MAX The number of threads per process. Value: 64". (Other versions, such as SuSv2, say the same thing.) – David Schwartz Nov 22 '11 at 17:32
This is a limitation from another time. I could run my custom game server using 2 threads per client and have 3000 simultaneaous players on a first-price machine. So none of this is relevant nowdays. I will move to another host which let me install the standard debian, not a rigged version. – Joel Nov 22 '11 at 17:36
@KarlP : It is not unreasonable to want 100,000 tasks to coexist -- as you point out, Erlang makes that easy. It is unreasonable to want 100,000 POSIX threads to coexist when POSIX says you get 64 of them. POSIX threads are, by intentional design, heavy. They are just not the right tool for this job. – David Schwartz Nov 22 '11 at 19:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.