Process Components: Memory Layout
What You'll Learn
You'll discover how a process's memory is organized into four distinct regions, each with a specific purpose.
The Four Sections of Process Memory
When a program becomes a running process, the operating system organizes its memory into four main sections. Think of these like different rooms in a house—each serves a unique function:
1. Text (Code) Section
This region holds the actual compiled program instructions—the machine code your CPU executes. It's typically read-only to prevent accidental modification of the program itself. Like a recipe book you follow but don't write in.
2. Data Section
This stores global and static variables that exist for the entire lifetime of the program. Variables declared outside functions live here. The data section has two parts:
- Initialized data: variables with initial values
- Uninitialized data (BSS): variables declared but not yet assigned
3. Heap
The heap is a region for dynamic memory allocation. When your program needs memory at runtime (think: creating objects or allocating arrays of unknown size), it grows upward from a low address. The heap grows and shrinks as memory is allocated and freed during execution.
4. Stack
The stack stores local variables, function parameters, and return addresses. Each time a function is called, a new "frame" is pushed onto the stack. When the function returns, that frame is popped off. The stack typically grows downward from a high address, opposite the heap's direction.
Memory Layout Visualization
High Address
+------------------+
| Stack | ← grows downward
| ↓ |
+------------------+
| |
| (free space) |
| |
+------------------+
| ↑ |
| Heap | ← grows upward
+------------------+
| Data | (global/static)
+------------------+
| Text (Code) | (instructions)
+------------------+
Low Address
Key Takeaway: Every process has four memory sections—text for code, data for globals, heap for dynamic allocation, and stack for function calls—each serving a distinct purpose in program execution.