How Much Virtual Memory Does a Linux Thread Actually Use?
This article reviews Linux virtual memory layouts for 32‑bit and 64‑bit systems, explains the kernel and user address spaces, details how threads consume stack memory, and outlines both virtual‑memory and kernel‑parameter limits that affect the maximum number of threads a process can create.
Linux Virtual Memory Overview
In Linux, the virtual address space is divided into kernel space and user space. The size of each depends on the architecture: a typical 32‑bit system has a 1 GB kernel space at the top and 3 GB user space below, while a 64‑bit system reserves 128 TB for both kernel and user spaces at the extremes, leaving the middle region undefined.
32‑Bit Virtual Memory Layout
The user‑space memory from low to high addresses consists of several segments:
0x00000000‑0x08048000: a reserved, non‑accessible region (e.g., NULL pointers).
Code segment: contains executable binary code.
Data segment: holds initialized static constants and global variables.
BSS segment: holds uninitialized static and global variables.
Heap segment: dynamically allocated memory that grows upward.
A gap above the heap for heap expansion.
Memory‑mapped segment: holds shared libraries, shared memory, etc., also growing upward.
Stack segment: stores local variables and call contexts; default size is typically 8 MB but can be configured.
Both the heap and memory‑mapped regions are allocated dynamically using functions such as malloc() or mmap().
64‑Bit Virtual Memory Layout
While a 64‑bit pointer can theoretically address 2^64 bytes (16 EB), current implementations use only 48 bits, giving a usable virtual address space of 256 TB. The layout mirrors the 32‑bit design, with kernel and user spaces each occupying 128 TB at the high and low ends respectively.
Virtual Memory Cost of Creating a Thread
Each thread requires its own stack. By default Linux allocates an 8 MB stack per thread, which can be inspected with ulimit -a:
Factors Limiting the Number of Threads per Process
Virtual‑memory limit : The total virtual address space available to the process.
System‑parameter limits : Kernel settings that cap the total number of threads system‑wide.
Virtual‑Memory Limit
32‑bit systems : A process has 4 GB of virtual space, 1 GB reserved for the kernel, leaving 3 GB for user space. With an 8 MB stack per thread, roughly 380 threads can be created. Reducing the stack size (e.g., ulimit -s 512) allows more threads. ulimit -s 512 64‑bit systems : User space can be up to 128 TB. Assuming an 8 MB stack, a single process could theoretically create over 10 million threads, so virtual‑memory is not the limiting factor.
System‑Parameter Limits
Even if virtual memory permits many threads, Linux imposes kernel parameters that restrict thread creation: /proc/sys/kernel/threads-max: Maximum number of threads the system supports (default ≈ 14553). /proc/sys/kernel/pid_max: Maximum PID value, affecting total processes/threads (default ≈ 32768). /proc/sys/vm/max_map_count: Maximum number of VMA (virtual memory areas) a process may have (default ≈ 65530).
Summary
On 32‑bit Linux, user‑space virtual memory is limited to 3 GB; with the default 8 MB stack, a process can create about 380 threads unless the stack size is reduced.
On 64‑bit Linux, user‑space virtual memory is vast (128 TB), so thread creation is limited by kernel parameters and performance considerations rather than virtual‑memory size.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
