Ady Wicaksono Daily Activities

I can not create more than 255 threads on Linux. What is the solutions?

with 4 comments

On kernel 2.4, you could only use up to 2 Gbytes (or actually 2^31 bytes ~ 2147483648 bytes) of Linux Virtual memory, even if you have 4 Gbytes memory or more :D.

If you want more than 2 Gbytes, you need to change the split of userspace/kernelspace
from 2G/2G (as default) of common Linux distro to something else like 3G/1G or maybe
4G/4G if you have 8 Gbytes RAM.

On kernel 2.6, you could use up to 2.8 Gbytes. Bigger right? But I need more.

I try to create a simple thread application, that simply count how much thread i could create?
Uh, i could only create up to 255 thread…….. What Is Problem?

From, I found that by default, Linux 2.4 set the stack size of each thread to 8Mbytes (equiv to 8 * 1024 * 1024 = 8388608 bytes = 2^23 bytes).
You could use “ulimit -a” command to check the amount of stack size like this
———- Cut Here ———-
$ ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
max locked memory (kbytes, -l) unlimited
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) unlimited
virtual memory (kbytes, -v) unlimited
———- Cut Here ———-

When you have 8Mbytes for each thread stack size, then you will hit this limit 2^(31-23) ~ 2^8 ~ 256 thread.

So, the solutions is REDUCE the THREAD STACK SIZE !!!. However reducing the stack size is dangerous, if any thread of a program overflows its stack the program will malfunction and probably crash.
Worse, there is no general way to know what the required stack size is!
You have to carefully design your program to use a bounded amount of stack in each thread.

How to do that? Again you could use :
1. “ulimit” command, for example ulimit -s 192 will reduce the stack size from 8192 kbytes to 192kbytes.

2. internally on your C application call pthread_attr_setstacksize().

3. internally on your Java Application you may use “-Xss” to set the stack size.

Now, how many thread could be created? For example you set thread stack size to 196608 bytes, and you have

up to 2147483648 bytes virtual memory (2Gbytes) then you can create 2147483648/196608 ~ 10922 threads.

But, be warn with maximum open files 🙂 of each user (default is 1024), set it using
“ulimit -n ” first or you only have 1024 thread 😀 .

Written by adywicaksono

July 10, 2007 at 5:04 pm

Posted in Linux

4 Responses

Subscribe to comments with RSS.

  1. Greetings I’m new here
    And it looks like a great forum, so just wanted to say hello! :):):)
    And looking forward to participating.
    Going on vacation for a few days, so i’ll be back


    December 18, 2008 at 1:39 am

  2. If you want to see a reader’s feedback 🙂 , I rate this post for four from five. Detailed info, but I just have to go to that damn yahoo to find the missed parts. Thank you, anyway!

    Heartburn Home Remedy

    April 15, 2009 at 12:37 pm

  3. I noticed that this is not the first time at all that you mention the topic. Why have you chosen it again?


    April 22, 2009 at 6:42 am

  4. tks for the effort you put in here I appreciate it!


    July 20, 2009 at 2:59 pm

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: