maximal number of threads

265 views
Skip to first unread message

Chris

unread,
Oct 5, 2010, 5:04:18 AM10/5/10
to android-ndk
Hello All,

I would like to determine, how many pThreads can be active on Android.
For that, I wrote a little application that creates threads until the
creation fails. I tested the app on a HTC phone and the result was 801
pThreads. As much as I know, the creation of threads fails if the
system runs out of resources, especially out of (virtual) memory.
Whenever a pThread is created, a thread stack has to be allocated and
I think that the default stack size is 1MB.

Question 1: if the stack size is 1MB and if it is possible to create
801 threads, does it mean that the virtual memory for user apps is
~800MB ? The phone only has 192MB of RAM.

Question 2: how big is the total virtual memory on Android and what
kind of memory management is used? I can not imagine how paging should
work on a phone.

When I created as many threads as possible I reached 801 threads with
the default stack size (1MB). I thought that if I change the stack
size to the half it should be possible to create the double amount of
threads, but the number of created threads stays the same. The same
happens, if I change the stack size to 2MB or 4MB. Its always 801
threads. (on Ubuntu the maximal number of threads does change if the
stack size is changed)

Question 3: how can that be that the number of threads is always 801,
independent of the thread's stack size? It seams that the virtual
memory is bigger than 800MB if it is possible to create 801 threads
with a stack size of 4MB but I find it curious that such a big number
is possible on a mobile device.

with the hope for many answers,
thanks,
Chris

fadden

unread,
Oct 5, 2010, 5:53:47 PM10/5/10
to android-ndk
On Oct 5, 2:04 am, Chris <christian.k...@gmail.com> wrote:
> Question 1: if the stack size is 1MB and if it is possible to create
> 801 threads, does it mean that the virtual memory for user apps is
> ~800MB ? The phone only has 192MB of RAM.

From build/core/prelink-linux-arm.map:

# 0xC0000000 - 0xFFFFFFFF Kernel
# 0xB0100000 - 0xBFFFFFFF Thread 0 Stack
# 0xB0000000 - 0xB00FFFFF Linker
# 0xA0000000 - 0xBFFFFFFF Prelinked System Libraries
# 0x90000000 - 0x9FFFFFFF Prelinked App Libraries
# 0x80000000 - 0x8FFFFFFF Non-prelinked Libraries
# 0x40000000 - 0x7FFFFFFF mmap'd stuff
# 0x10000000 - 0x3FFFFFFF Thread Stacks
# 0x00000000 - 0x0FFFFFFF .text / .data / heap

It doesn't always work out quite this way, but the key point is that
the virtual address space is not a wide-open world. To see where the
space is actually going, put a "sleep" call in your program (or
SIGSTOP it) and then look at /proc/<pid>/maps to see how space is laid
out.

FWIW, on one of my devices I just created 2000 threads from a quick
test program. Threads allocated from Dalvik have the 1MB native stack
and a 12KB interpreter stack.

> Question 2: how big is the total virtual memory on Android and what
> kind of memory management is used? I can not imagine how paging should
> work on a phone.

See map above. Paging works exactly as you'd expect. Swapping, on
the other hand, is totally disabled.
Reply all
Reply to author
Forward
0 new messages