In Linux the maximum number of threads is defined as max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE);
, and can be retreived by calling cat /proc/sys/kernel/threads-max
. This returns around 14,000 for my raspberry Pi 3. However, when I just create threads in a loop with pthread_create()
,(which are empty), I can create only 250, before I getENOMEM (Cannot allocate memory)
.
Now I looked at the default stack that is allocated to a process or thread, and that is 8192k. So at around 250 threads I would be using 2GB memory. However, in my mind this also does not add up, because calling free -m
shows I got total of 1GB memory.
Since I have 1GB of ram, I expected to be able to only create 125 threads at maximum, not 250, and not 14000.
Why can I create 250 threads?