OS Notes
Introduction to memory management in operating systems covering memory hierarchy, address binding, logical vs physical addresses, and the role of the Memory Management Unit (MMU).
Introduction
Every program you run needs memory. Your web browser needs memory to store the web page content. Your music player needs memory for the audio buffer. Your IDE needs memory for the code you are editing. But here is the challenge: physical RAM is limited and expensive, while programs are greedy — they always want more memory than is available.
Memory management is how the operating system allocates, tracks, and reclaims memory for running processes. It is like a landlord managing apartments in a building — assigning units to tenants, ensuring no one occupies someone else's space, and reclaiming apartments when tenants leave. Good memory management means efficient use of every byte; poor management means crashes, slowdowns, and wasted resources.
Memory Hierarchy
Computer systems use a hierarchy of memory types, trading off speed for capacity and cost:
The OS primarily manages the main RAM level, deciding which process gets which portion of physical memory and using the disk as overflow (virtual memory/swap space).
Why Memory Management is Complex
Several challenges make memory management non-trivial:
- Multiple processes need memory simultaneously — How to divide limited RAM fairly?
- Protection — Process A must not be able to read/write Process B's memory
- Relocation — Programs cannot know in advance where in memory they will be loaded
- Sharing — Some memory (shared libraries) should be accessible by multiple processes
- Fragmentation — Memory becomes divided into unusable small pieces over time
Logical vs Physical Addresses
This distinction is fundamental to understanding memory management:
Logical Address (Virtual Address): The address that a process "sees" and uses in its code. Generated by the CPU during execution. Each process has its own logical address space starting from 0.
Physical Address: The actual location in RAM hardware. This is what appears on the memory bus and addresses actual memory chips.
The Memory Management Unit (MMU)
The MMU is hardware that translates logical addresses to physical addresses at runtime. Every memory access goes through the MMU.
In simple systems, the MMU uses a base register (also called relocation register). The physical address = logical address + base register value. In modern systems, the MMU uses page tables for more sophisticated translation (covered in the Paging topic).
Address Binding
Programs go through several stages before execution. Addresses can be "bound" (fixed to specific locations) at different stages:
Compile Time
If the exact memory location is known at compile time, the compiler generates absolute addresses. Example: MS-DOS .COM files were always loaded at address 0x100. If the starting location changes, the program must be recompiled.
Load Time
If the memory location is not known at compile, the compiler generates relocatable code. When the program is loaded into memory, the loader adjusts all addresses based on the actual loading position. Binding happens once at load time.
Execution Time (Runtime)
If a process can be moved during execution (to compact memory or support virtual memory), binding must be deferred to runtime. The MMU hardware performs translation on every memory access. This is the most flexible approach and what modern systems use.
Dynamic Loading
With dynamic loading, a routine is not loaded into memory until it is called. This saves memory for large programs with many seldom-used routines (like error handling code or rare features). The main program is loaded, and other routines are loaded on demand.
Dynamic Linking and Shared Libraries
Instead of including library code in every program, the OS uses shared libraries (DLLs on Windows, .so files on Linux). Multiple programs share a single copy of the library in memory.
Without shared libraries
Program A: [code][libc copy][libm copy] → 15 MB
Program B: [code][libc copy][libm copy] → 12 MB
Total RAM used for libraries: 27 MB
With shared libraries
Program A: [code][ptr to libc][ptr to libm] → 3 MB
Program B: [code][ptr to libc][ptr to libm] → 2 MB
Shared in RAM: [libc][libm] → 5 MB
Total RAM used: 10 MB (saved 17 MB!)
Memory Protection
The OS must prevent processes from accessing memory that does not belong to them. This is enforced through:
- Base and limit registers: Each process has a base (starting address) and limit (size). Any access outside this range causes a trap (segmentation fault).
- Page-level protection: Each page has permission bits (read, write, execute)
- Hardware enforcement: The MMU checks every access — violations trigger exceptions handled by the OS
Real-World Analogy
Memory management is like managing hotel rooms. The hotel (RAM) has a fixed number of rooms (memory addresses). The front desk (OS) assigns rooms to guests (processes), ensures no guest enters another's room (protection), and may move guests to different rooms if needed (relocation). The hotel directory (page table) maps guest names (logical addresses) to actual room numbers (physical addresses). When the hotel is full, some guests may be temporarily moved to an overflow building (swap space on disk).
Key Takeaways
- Memory management allocates, protects, and reclaims RAM for concurrent processes
- The memory hierarchy trades speed for capacity — the OS manages the RAM level
- Logical addresses (process view) differ from physical addresses (actual RAM locations)
- The MMU hardware translates logical to physical addresses on every memory access
- Address binding can occur at compile time, load time, or runtime (most flexible)
- Dynamic loading and shared libraries reduce memory waste
- Memory protection ensures processes cannot corrupt each other's data
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Memory Management Introduction.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Operating Systems topic.
Search Terms
operating-systems, operating systems, operating, systems, memory, management, introduction, memory management introduction
Related Operating Systems Topics