Skip to content

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 address

The 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 ​

SegmentWhat goes in itWhen its size is decidedCharacteristics
Text (Code)Machine instructions to executeAt compile timeRead-only. Multiple instances of the same program can share this segment
DataGlobal and static variables with initial valuesAt compile timeStays until the program ends
BSSGlobal and static variables without initial valuesAt compile timeFilled with 0 at run time
HeapData created during execution, like malloc, new or Python objectsAt run timeMust be cleaned up by the developer (or the GC). Grows toward higher addresses
StackFunction parameters, local variables, return addressesAt run timeDisappears 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.

text
One process
┌─────────────────────────────────────┐
│ Text │ Data │ BSS │ Heap  ← all shared │
├─────────────────────────────────────┤
│ Stack(thread1) │ Stack(thread2) │ ... │ ← separate per thread
└─────────────────────────────────────┘

Summary ​

SegmentRoleManagement · Size
StackFunction call records, local variables (grows down)Automatic (gone when the function ends)
HeapDynamic allocation space (grows up), where leaks happenManual (the developer frees it)
BSSUninitialized global variables (filled with 0 at run time)Fixed at compile time
DataInitialized global variables, static variablesFixed at compile time
TextThe executable code itself; read-only, so instances of the same program can share itFixed at compile time
ThreadsOnly the Stack is separate; other segments are shared among threads of the same process

Source: Codyssey-B1/B4-2