Memory Layout (Process Memory Layout)
When you run a program, the operating system gives it a memory space that only that process uses. This is called the process address space, and inside it is divided by purpose into the Text (Code), Data, BSS, Heap and Stack segments.
Analogy: Floors of an Apartment Building
Think of the memory a process uses as an apartment building. Each floor has a different role.
High address
┌─────────────────────────┐
│ Stack │ ← 5th floor: function call records, local variables
│ (grows down ↓) │ (automatic - gone when the function ends)
├─────────────────────────┤
│ ↕ │ ← Free space (Stack and Heap grow toward each other)
├─────────────────────────┤
│ Heap │ ← 4th floor: dynamic allocation **where leaks happen!**
│ (grows up ↑) │ (manual - the developer must free it)
├─────────────────────────┤
│ BSS │ ← 3rd floor: uninitialized global variables
├─────────────────────────┤
│ Data │ ← 2nd floor: initialized global variables
├─────────────────────────┤
│ Text │ ← 1st floor: the executable code itself
└─────────────────────────┘
Low addressThe leak in this document happens on the 4th floor (Heap)! It is like a building that filled up because stuff kept piling in and nobody cleared it out.
A Closer Look at Each Segment
| Segment | What goes in it | When its size is decided | Characteristics |
|---|---|---|---|
| Text (Code) | Machine instructions to execute | At compile time | Read-only. Multiple instances of the same program can share this segment |
| Data | Global and static variables with initial values | At compile time | Stays until the program ends |
| BSS | Global and static variables without initial values | At compile time | Filled with 0 at run time |
| Heap | Data created during execution, like malloc, new or Python objects | At run time | Must be cleaned up by the developer (or the GC). Grows toward higher addresses |
| Stack | Function parameters, local variables, return addresses | At run time | Disappears automatically when the function ends. Grows toward lower addresses |
- Heap and Stack grow toward each other with free space in between. If you call functions too deeply (e.g. recursion that never ends), the Stack exceeds its fixed size and you get a stack overflow. If you use the Heap and never clean it up, you get a memory leak.
Why Split Memory Like This
The key idea is to save memory by sharing what can be shared, and to manage data with different characteristics in different ways.
- Why Code is kept separate: For the same program, the instructions are identical. Even if you open many Chrome windows, only one copy of the code needs to sit in memory for all of them to read. So it is managed separately as read-only.
- Why Stack and Data are separate: Functions and local variables appear and disappear in "last called, first finished" order (LIFO), so a stack structure fits perfectly. Global variables used by many functions, on the other hand, must survive after a function ends. Putting them once in the Data segment instead of the stack, and letting everyone refer to them, saves memory.
- Why Heap is kept separate: Data whose size is only known at run time (e.g. how many files a user uploads) cannot have space reserved in advance. You need a separate space to borrow when needed and give back when done.
Which Segments Do Threads Share
Threads in one process each get their own Stack only, and share Text, Data, BSS and Heap. For each thread to run different functions at the same time, the function call records (Stack) must be separate. See Processes and Threads for details.
One process
┌─────────────────────────────────────┐
│ Text │ Data │ BSS │ Heap ← all shared │
├─────────────────────────────────────┤
│ Stack(thread1) │ Stack(thread2) │ ... │ ← separate per thread
└─────────────────────────────────────┘Summary
| Segment | Role | Management · Size |
|---|---|---|
| Stack | Function call records, local variables (grows down) | Automatic (gone when the function ends) |
| Heap | Dynamic allocation space (grows up), where leaks happen | Manual (the developer frees it) |
| BSS | Uninitialized global variables (filled with 0 at run time) | Fixed at compile time |
| Data | Initialized global variables, static variables | Fixed at compile time |
| Text | The executable code itself; read-only, so instances of the same program can share it | Fixed at compile time |
| Threads | Only the Stack is separate; other segments are shared among threads of the same process |
Source: Codyssey-B1/B4-2
