Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximum size of stack of multi threaded process

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?

like image 203
MankPan Avatar asked Oct 25 '25 14:10

MankPan


2 Answers

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.

like image 161
Jeremy Friesner Avatar answered Oct 27 '25 04:10

Jeremy Friesner


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.

like image 41
user3344003 Avatar answered Oct 27 '25 03:10

user3344003