Memory Regions
#memory | #lowlevel | #cache
When discussing memory layout (or hierarchy) in the context of programming and computer systems, there are several types that play different roles. The most commonly referenced types are Heap
and Stack
, but there are other types as well. Let's get into this small adventure and have an overview of the most relevant types of memory regions (or segments).
Understanding Memory in C language
When writing C programs, memory management is one of the most important concepts to grasp. If you've ever encountered segmentation faults, memory leaks, or stack overflows, chances are that memory was mismanaged. In lower-level languages like C, you are responsible for handling memory efficiently—unlike languages like Python or Java, which use automatic garbage collection.
But before mastering malloc()
and free()
, you need to understand the region your data lives in the memory layout. In this blog series, we will break down these memory regions, explore real-world examples, and discuss common mistakes that developers make when working with memory. Even in managed languages like Java, or Python, understanding memory allocation patterns can help optimize performance by reducing unnecessary allocations and minimizing pressure on the garbage collector.
Whether you're a beginner looking to strengthen your fundamentals or an experienced developer aiming to refine your memory management skills, this series will provide valuable insights into how memory works under the hood.
Computer Memory Layout
+------------------------------+
│ << CPU Space >> │ <- Processor (Not RAM)
0xFFFFFFFF +------------------------------+
│ Registers │ <- Static, Fastest (Not RAM)
+------------------------------+
│ CPU Cache (L1, L2, L3, L4) │ <- Static, Fast Memory (Not RAM)
+------------------------------+
+------------------------------+
│ High Memory (ROM, MMIO) │ <- Static (Not RAM)
0xC0000000 +------------------------------+
│ << Kernel Space >> │
│ Code, Data, Drivers │ <- Static (RAM)
0xBFFFFFFF +------------------------------+
│ << User Space >> │
│ Stack (grows down) │ <- Dynamic (RAM)
│ Heap (grows up) │ <- Dynamic (RAM)
+------------------------------+
│ Code Segment (Text) │ <- Static (RAM)
│ Data Segment (Init, BSS) │ <- Static (RAM)
0x08048000 +------------------------------+
│ << Reserved Area >> │ <- Some parts may be RAM
0x00000000 +------------------------------+
│ Low Memory (BIOS) │ <- Static (Not RAM)
+------------------------------+
+------------------------------+
│ << Virtual Space >> │
| Storage (SSD, HDD, etc.) | <- Not part of physical memory
+------------------------------+
Legend
- CPU: Central Processing Unit
- CPU Cache (L1, L2, L3, L4): L stands for Level (different sizes and speeds)
- ROM: Read-Only Memory (Firmware, BIOS)
- MMIO: Mapped Hardware I/O (Hardware Device, GPUs, NICs, etc.)
- RAM: Random-Access Memory (OS, Applications)
- Reserved Area: Allocated for system-level data such as interrupt vector tables (IVT) and essential OS structures.
- Low Memory: The lower memory region used for BIOS data, hardware initialization, and critical low-level information.
- BSS: Block Started by Symbol
- BIOS: Basic Input/Output System. A firmware that initializes and tests hardware during the booting process of a computer and provides runtime services for operating systems and programs.
Info
MMIO is a technique where hardware devices (such as GPUs, network cards, and disk controllers) are mapped into the CPU's memory space. Instead of using special CPU instructions for I/O, the CPU can read from and write to these device registers as if they were regular memory addresses.
Key points about MMIO:
- Used by hardware peripherals (GPUs, NICs, storage controllers, etc.)
- Mapped into the memory address space (often in high memory)
- Accessed using standard memory instructions (mov, load, store)
- Faster than port-mapped I/O (because it avoids special CPU instructions)
Key takeways
- CPU is NOT RAM, but it has internal memory (Registers & Cache);
- L1 & L2 cache are always inside the CPU (L1 is per core, L2 is per core or shared);
- L3 cache is usually inside the CPU, but L4 cache (if present) can be external (e.g., eDRAM on some Intel CPUs);
- Some parts of Kernel Reserved Areas might be RAM, but others are firmware, MMIO, or system-reserved regions;
- Virtual Space is NOT physical memory, but it can be used for memory swap (paging) to extend RAM;
Memory Regions Blogs
Please use the table bellow to navigate between the different memory region blogs. Each link will open in a new tab by default to improve the topics navigation experience.
Type | Purpose | Size | Speed |
---|---|---|---|
OS Kernel1 | User code cannot read from nor write to these address | - | - |
Stack | Local variables, function call management | Small | Very fast |
Heap | Dynamically allocated objects | Large | Slower |
Static/Global | Global/static variables (initialized or not) | Medium | Fast |
1Last time Microsoft gave Kernel access to CrowdStrike, it caused a major outage affecting millions of users.