I can not create more than 255 threads on Linux. What is the solutions?
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 http://www.kegel.com/stackcheck/, 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 :D .