Calculate thread stack size requirements and total stack memory for multi-threaded processes.
Each thread in a multi-threaded process gets its own stack, sized by ulimit -s (default 8MB on most Linux distributions) or explicitly via pthread_attr_setstacksize(). With hundreds or thousands of threads, total stack memory reservation becomes substantial — 200 threads × 8MB = 1.6GB of virtual address space. Reducing per-thread stack size for threads with known shallow call depths can significantly reduce the memory footprint of high-concurrency servers.
Total stack memory
total_stack = (per_thread_stack + guard_page) × thread_count
Usually 8MB (8192KB), set by ulimit -s. The actual default comes from the RLIMIT_STACK resource limit and can be overridden per-thread via pthread_attr_setstacksize() before creating the thread.
When running many threads (hundreds+) that have shallow call stacks and don't use large stack-allocated arrays. Web servers, thread pools, and event-driven workers often safely use 256KB-1MB stacks.