4

As per my understanding

  1. each thread of a process gets a stack, while there's typically only one heap for the process.
  2. There is default stack max size limit set by OS.

    1. Windows-64 bit : 1MB
    2. Linux-64 bit : 8MB

Is this limit applicable at process level or each thread can have 1MB/8MB stack?

And what happens to the memory allotted to stack after thread exit?

2 Answers 2

6

each thread of a process gets a stack, while there's typically only one heap for the process.

That's correct.

Is this limit applicable at process level or each thread can have 1MB/8MB stack?

Each thread gets its own stack; the stack-size-limit is per-thread (i.e. it is not a shared limit for all threads in the process)

And what happens to the memory allotted to stack after thread exit?

The memory pages are released and become available for use by other code in the future.

Sign up to request clarification or add additional context in comments.

3 Comments

"The memory pages are released and become available for use by other code in the future. On exit of a thread this only happens for a detached thread, for an undetached thread (which at least for POSIX threads is the default) this happens when the thread is joined.
@alk There is nothing like detached or undetached thing in the underlying tech of a thread, the detached/undetached stuff is at the C level.
@MichaelChourdakis Nor do threads have stacks - something has to manage the stack for a thread. The stack is managed by the same mechanisms that manage detached/undetached.
1

each thread of a process gets a stack, while there's typically only one heap for the process.

The former is true. The latter is false. Processes frequently have multiple heaps, especially when linking in 3d party code.

Is this limit applicable at process level or each thread can have 1MB/8MB stack?

Per thread.

And what happens to the memory allotted to stack after thread exit?

Typically they will remain allocated to the process until the process exits and the address space no longer exists.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.