Memory Management
Memory management is one of the most important functions of an Operating System.
Memory management is the process by which the OS manages main memory (RAM), allocates memory to processes, tracks memory usage, and frees memory when it is no longer needed.
Think of RAM as a limited workspace. The OS decides which process gets which part of RAM, when it gets it, and when that space is released.
1. Why Memory Management Is Needed
Suppose your computer has:
RAM = 8 GB
And you run:
Chrome → 2 GB VS Code → 1 GB Linux VM → 2 GB Spotify → 500 MB Other apps → 1.5 GB
The OS must keep track of all these allocations.
It must answer:
- Which memory is free?
- Which memory is occupied?
- Which process owns it?
- Where should a new process be loaded?
- When should memory be released?
- How can multiple processes use RAM safely?
- How can a process use more memory than physically available?
This is the job of memory management.
2. Main Memory / RAM
Main memory is the memory directly used by the CPU for executing programs.
CPU ↓ Main Memory (RAM) ↓ Programs / Processes
RAM is:
- Fast
- Volatile
- Limited
- Directly accessible by the CPU
When a program executes, it generally needs to be loaded into memory.
For example:
Program stored on SSD ↓ Load ↓ RAM ↓ CPU ↓ Execution
3. Memory Management Responsibilities
The OS performs several important tasks.
1. Memory allocation
Give memory to processes.
P1 → 2 MB P2 → 4 MB P3 → 1 MB
2. Memory deallocation
Take memory back when a process finishes.
P2 terminates ↓ 4 MB becomes free
3. Memory tracking
The OS keeps track of:
Used memory Free memory Allocated regions Process ownership
4. Protection
One process should not normally access another process's memory illegally.
P1 Memory ✕ P2 Memory
5. Sharing
Sometimes processes intentionally share memory.
P1 ──┐ ├── Shared Memory P2 ──┘
6. Address translation
The OS and hardware translate addresses used by programs into actual physical RAM locations.
7. Virtual memory management
The OS can allow programs to use an address space larger than physical RAM by using secondary storage.
4. Memory Management Structure
A simplified view:
CPU | ↓ Logical / Virtual Address | ↓ Memory Management Unit (MMU) | ↓ Physical Address | ↓ RAM
The MMU (Memory Management Unit) is hardware that helps translate addresses.
5. Logical Address
A logical address is an address generated from the point of view of a running program.
It is also commonly called a virtual address.
Example:
Program says: Read data from address 5000
The program does not necessarily know the actual RAM location.
6. Physical Address
A physical address is the actual location in physical RAM.
For example:
Logical address 5000 ↓ MMU ↓ Physical address 125000
So:
Logical Address → Physical Address
7. Address Translation
The translation process can be represented as:
Program | ↓ Logical Address | ↓ MMU | ↓ Physical Address | ↓ RAM
This provides several benefits:
- Process isolation
- Memory protection
- Virtual memory
- Flexible memory allocation
8. Memory Protection
Memory protection prevents one process from accidentally or maliciously accessing another process's memory.
Suppose:
RAM +------------------+ | Operating System | +------------------+ | Process A | +------------------+ | Process B | +------------------+ | Process C | +------------------+
Normally:
Process A → its own memory ✓ Process A → Process B memory ✗ Process A → OS protected memory ✗
Hardware support such as the MMU, page tables, protection bits, and privilege levels helps enforce this.
9. Process Memory Layout
A typical process has several important memory regions.
High Address +----------------------+ | Stack | | ↓ | +----------------------+ | | | Free / unused | | | | ↑ | +----------------------+ | Heap | +----------------------+ | Data | +----------------------+ | Code | +----------------------+ Low Address
Let's understand them.
10. Code / Text Segment
Contains the program's executable instructions.
Example:
int main() { printf("Hello"); }
The machine instructions generated from this program are stored in the code/text segment.
Typically, this region is protected from modification.
11. Data Segment
Stores initialized global and static variables.
Example:
int x = 10; static int y = 20;
These may be placed in the data segment.
12. BSS Segment
BSS generally contains uninitialized or zero-initialized global/static variables.
Example:
int x; static int y;
The OS/runtime initializes them appropriately.
So a simplified process layout is:
Code Data BSS Heap Stack
13. Heap
The heap is used for dynamically allocated memory.
Example in C:
int *p = malloc(100 * sizeof(int));
The dynamically allocated memory comes from the heap.
In C++:
int *p = new int[100];
Again, dynamically allocated storage is associated with the heap.
The heap generally grows dynamically as the program requests memory.
14. Stack
The stack is used for function calls, local variables, return information, and related execution state.
Example:
void test() { int x = 10; }
x is typically stored in stack memory.
When the function returns:
test() ↓ stack frame created ↓ x = 10 ↓ function returns ↓ stack frame removed
15. Stack vs Heap
| Stack | Heap |
|---|---|
| Function calls | Dynamic allocation |
| Local variables | Dynamically created objects/data |
| Automatically managed | Managed through allocator/runtime |
| Usually faster | Generally more complex |
| Limited in size | Usually much larger |
| LIFO behavior | No simple LIFO requirement |
Example:
void test() { int x = 10; // typically stack int *p = malloc(...); // heap }
16. Memory Allocation
When a process needs memory, the OS/system memory manager must provide an appropriate region.
For example:
RAM: +----------------+ | P1 | +----------------+ | Free | +----------------+ | P2 | +----------------+ | Free | +----------------+
A new process arrives:
P3 needs 2 MB
The memory manager finds a suitable free region.
Memory allocation is covered more deeply in your next topic: Memory Allocation.
17. Contiguous Memory Allocation
In contiguous allocation, a process is placed into one continuous region of physical memory.
Example:
RAM +-------------+ | OS | +-------------+ | P1 | +-------------+ | P2 | +-------------+ | P3 | +-------------+ | Free | +-------------+
Each process occupies one contiguous block.
This approach is simple but can cause fragmentation.
18. Fragmentation
Fragmentation means memory is wasted because of how memory is allocated.
There are two major types:
Fragmentation | +------ Internal | +------ External
19. Internal Fragmentation
Internal fragmentation occurs when allocated memory contains some unused space inside the allocated block.
Example:
Process needs = 6 KB Allocated block = 8 KB +----------------+ | Process 6 KB | | Unused 2 KB | +----------------+
The 2 KB cannot easily be given to another process because it is inside the allocated block.
Therefore:
Internal fragmentation = wasted space inside an allocated block.
20. External Fragmentation
External fragmentation occurs when enough total free memory exists, but it is divided into small separate holes.
Example:
+-------+ | P1 | +-------+ | Free | 2 MB +-------+ | P2 | +-------+ | Free | 3 MB +-------+ | P3 | +-------+ | Free | 4 MB +-------+
Total free memory:
2 + 3 + 4 = 9 MB
Suppose a process needs:
8 MB contiguous
It cannot fit into any single hole.
So:
External fragmentation = free memory exists, but it is scattered into separate holes.
21. Memory Allocation Techniques
Some traditional allocation strategies include:
First Fit
Allocate the first suitable free block.
Free: 5 MB → 10 MB → 3 MB → 8 MB Request = 7 MB First suitable block = 10 MB
Best Fit
Choose the smallest block that can satisfy the request.
Free: 5 MB → 10 MB → 3 MB → 8 MB Request = 7 MB Best fit = 8 MB
Worst Fit
Choose the largest available block.
Free: 5 MB → 10 MB → 3 MB → 8 MB Request = 7 MB Worst fit = 10 MB
These will be covered in detail in Memory Allocation.
22. Paging
Paging divides memory into fixed-size blocks.
Logical memory:
Pages +----+ | P0 | +----+ | P1 | +----+ | P2 | +----+ | P3 | +----+
Physical memory:
Frames +----+ | F0 | +----+ | F1 | +----+ | F2 | +----+ | F3 | +----+ | F4 | +----+
A page can be placed into any available frame.
For example:
Page 0 → Frame 3 Page 1 → Frame 0 Page 2 → Frame 4 Page 3 → Frame 1
A page table keeps track of this mapping.
Paging greatly reduces external fragmentation.
It is an important part of modern virtual memory systems.
23. Segmentation
Segmentation divides a program according to logical units.
For example:
Program | +── Code | +── Data | +── Stack | +── Heap
Each is treated as a segment with its own size and address information.
Unlike paging, segments are variable-sized.
Segmentation is covered separately in your roadmap.
24. Virtual Memory
One of the most important memory-management concepts.
Virtual memory allows a process to have a large virtual address space even when physical RAM is limited.
Example:
RAM = 8 GB Program virtual address space ↓ Can be much larger than RAM
The OS can keep some parts in RAM and other parts on storage.
Virtual Memory | +-------+-------+ | | ↓ ↓ RAM Disk/SSD
When required data is not currently in RAM, it can be brought into memory.
25. Swapping
Swapping involves moving processes or memory contents between RAM and secondary storage.
Conceptually:
RAM | | swap out ↓ Disk | | swap in ↓ RAM
Example:
RAM is full P1 → RAM P2 → RAM P3 → RAM P4 needs memory OS may move some less-active data from RAM to disk
Then RAM becomes available.
26. Page Fault
In a paging-based virtual memory system, suppose a process accesses a page that is not currently in RAM.
This causes a:
Page fault
Flow:
CPU requests page ↓ Is page in RAM? / \ Yes No | | ↓ ↓ Continue Page Fault ↓ Load page from disk ↓ Update page table ↓ Continue
Page faults are normal in virtual-memory systems, but excessive page faults can seriously reduce performance.
27. Thrashing
Thrashing occurs when the system spends too much time moving pages between RAM and storage and too little time executing actual programs.
Example:
Page out ↓ Page in ↓ Page out ↓ Page in ↓ Page out ↓ Page in ↓ Very little useful execution
This can happen when processes have insufficient physical memory for their active working sets.
28. Memory Sharing
The OS can allow processes to share certain memory regions.
Example:
P1 ──────┐ ↓ Shared Memory ↑ P2 ──────┘
This is useful for:
- IPC
- Shared libraries
- Memory-mapped files
- Performance optimization
However, shared writable memory requires synchronization.
29. Memory Protection
The OS must prevent illegal memory access.
For example:
P1 memory: +----------+ | Code | | Data | | Heap | | Stack | +----------+ P2 should not directly access P1's private memory.
If a program attempts an invalid memory access, the hardware/OS may generate an exception such as a protection fault or segmentation fault.
30. Memory Management and Security
Memory management also has security implications.
The OS tries to prevent:
Process A ↓ Unauthorized access ↓ Process B memory
Modern systems use mechanisms such as:
- Virtual address spaces
- Page permissions
- User/kernel separation
- NX/execute-disable protections
- ASLR
- Memory isolation
These make unauthorized memory access more difficult.
31. Memory Management in a Modern OS
A simplified flow:
Program | ↓ Virtual Address | ↓ MMU | ↓ Page Table | ↓ Physical Address | ↓ RAM | +---+---+ | | ↓ ↓ Present Not Present | | ↓ ↓ Execute Page Fault | ↓ Disk / SSD | ↓ Load Page
32. Important Memory Management Terms
| Term | Meaning |
|---|---|
| RAM | Main physical memory |
| Logical address | Address generated by program |
| Physical address | Actual RAM address |
| MMU | Hardware that translates addresses |
| Page | Fixed-size block of virtual memory |
| Frame | Fixed-size block of physical memory |
| Page table | Maps pages to frames |
| Segment | Logical variable-size memory unit |
| Heap | Dynamic memory area |
| Stack | Function-call/local execution memory |
| Fragmentation | Wasted memory |
| Swapping | Moving memory contents between RAM and storage |
| Page fault | Required page is not currently in RAM |
| Thrashing | Excessive paging/swapping |
33. Memory Management vs Memory Allocation
These terms are related but not identical.
Memory Management
The whole process of managing memory.
Includes:
Allocation Deallocation Protection Address translation Paging Segmentation Virtual memory Swapping Sharing
Memory Allocation
Specifically focuses on:
Giving available memory space to processes/programs.
So:
Memory Management | +── Memory Allocation +── Paging +── Segmentation +── Virtual Memory +── Protection +── Address Translation +── ...
34. Advantages of Good Memory Management
Good memory management provides:
- Efficient RAM utilization
- Process isolation
- Memory protection
- Support for multitasking
- Virtual memory
- Better CPU utilization
- Controlled memory sharing
- Reduced memory waste
- Stable system operation
35. Complete Memory Management Picture
MEMORY MANAGEMENT | +--------------------+--------------------+ | | | ↓ ↓ ↓ Allocation Address Management Protection | | | ↓ ↓ ↓ First Fit Logical Address Process Isolation Best Fit Physical Address Permissions Worst Fit MMU | ↓ Virtual Memory | +-------------+-------------+ | | ↓ ↓ Paging Segmentation | ↓ Page Fault | ↓ Swapping | ↓ Thrashing
🧠 Exam Revision
Definition
Memory management is an operating system function responsible for managing main memory by allocating and deallocating memory, maintaining memory usage information, translating addresses, protecting memory, and supporting techniques such as paging, segmentation, and virtual memory.
Most important concepts:
Memory Management │ ├── Allocation / Deallocation ├── Logical Address ├── Physical Address ├── MMU ├── Process Memory Layout │ ├── Code │ ├── Data │ ├── BSS │ ├── Heap │ └── Stack ├── Memory Protection ├── Fragmentation │ ├── Internal │ └── External ├── Paging ├── Segmentation ├── Virtual Memory ├── Swapping ├── Page Fault └── Thrashing
Easy memory trick:
“Allocate → Translate → Protect → Share → Virtualize.”