Introduction to Address Spaces and Virtual Memory
Virtual Memory and Address Space
What early systems were like
-
Early computers had simple memory layouts:
-
OS stored at low physical addresses (e.g., 0 KB)
-
Only one program loaded after it
-
-
No abstraction or isolation
-
Program directly used physical memory
➡️ Easy for OS developers, but very limited.
Why virtual memory became necessary
As computers evolved:
✔ Multiprogramming
-
Multiple processes kept in memory simultaneously
-
OS switches CPU between them
-
Improves CPU utilization
✔ Time sharing
-
Many users interact with system concurrently
-
Need fast switching
-
Cannot save entire memory to disk each time (too slow)
➡️ Therefore, processes must stay in memory together.
This created two big problems:
-
Protection → processes must not access each other’s memory
-
Convenience → programs should still see simple memory layout
1️⃣ What is an Address Space?
An address space is the program’s view of memory — an abstraction created by the OS so programs can use memory easily.
Each running process thinks it has its own private memory, which includes:
-
Code segment → program instructions
-
Stack → function calls, local variables, parameters, return values
-
Heap → dynamically allocated memory (e.g.,
malloc,new) -
(plus static/global data, etc.)
๐ Together, these form the process’s memory state.
2️⃣ Layout of a Typical Address Space
In the example:
Key ideas:
-
Code is placed first because it usually doesn’t grow.
-
Heap and stack can grow/shrink while program runs.
-
They are placed at opposite ends so both can expand without immediately colliding.
-
This layout is a convention, not a strict rule.
3️⃣ Important Reality Check ⚠️
Even though a program thinks:
๐ This is only an illusion.
In reality:
-
The program might actually be stored somewhere else in physical memory
-
Example:
-
Program uses virtual address 0
-
OS translates it to physical address 320KB
-
This translation happens automatically using:
-
OS memory management
-
Hardware support (MMU, page tables, etc.)
4️⃣ What is Virtual Memory then?
Virtual memory = the OS creating the illusion that each process has its own large, private memory space, even though:
-
Physical memory is limited
-
Many processes share it simultaneously
So:
| What program sees | What actually happens |
|---|---|
| Private memory | Shared physical memory |
| Starts at address 0 | Loaded at arbitrary location |
| Large continuous space | Possibly scattered in RAM |
๐น Virtual vs Physical Addresses
The addresses a program uses are virtual addresses
Real RAM locations are physical addresses
Example:
Program thinks it loads from address 0
But actually stored at 320 KB in physical memory
The OS + hardware translate:
This translation is the essence of:
Virtual Memory5️⃣ Why do we need Virtual Memory?
This solves key problems:
✅ Process isolation
-
One process cannot read/write another’s memory
-
Prevents crashes and corruption
✅ Multiprogramming support
-
Multiple programs can stay in memory together
-
OS switches CPU between them efficiently
✅ Ease of programming
-
Programmers don’t need to worry about:
-
where memory physically sits
-
collisions with other processes
-
6️⃣ The Core Challenge (THE CRUX)
How can the OS give each process a large private address space while all processes share one physical memory?Solution:
✔ Use virtual addresses seen by programs
✔ Translate them to physical addresses using hardware + OS
This translation mechanism is the foundation of modern operating systems.
Note:
EVERY ADDRESS YOU SEE IS VIRTUAL
Ever write a C program that prints out a pointer? The value you see (some large number, often printed in hexadecimal) is a virtual address.
Ever wonder where the code of your program is found? You can print that out too, and yes, if you can print it, it also is a virtual address. In fact, any address you can see as a programmer of a user-level program is a virtual address.
It’s only the OS, through its tricky techniques of virtualizing memory, that knows where in the physical memory of the machine these instructions and data values lie. So never forget: if you print out an address in a program, it’s a virtual one—an illusion of how things are laid out in memory; only the OS (and the hardware) knows the real truth.
Example Program
Sample Output (64-bit Mac OS X machine)
Observation
From this output, you can see that:
-
Code comes first in the address space
-
Then the heap
-
The stack is located at the other end of the large virtual space
All of these addresses are virtual, and they will be translated by the OS and hardware to fetch values from their true physical memory locations.
Goals of Virtual Memory
Virtual memory is designed to let the operating system manage memory in a way that is easy for programs, efficient for the system, and safe for all processes. The main goals are:
1. Transparency (Illusion of Private Memory)
-
Virtual memory should be invisible to the running program.
-
Each program behaves as if it has its own private physical memory.
-
The OS and hardware secretly handle the sharing and mapping of real memory.
-
This creates the illusion that the program owns the entire memory space.
2. Efficiency (Fast and Space-Effective)
-
Virtualization should not slow programs down significantly.
-
The OS must manage memory in a time-efficient way (fast address translation).
-
It should also be space-efficient, avoiding excessive overhead for memory structures.
-
Hardware support (like TLBs) helps speed up address translation.
3. Protection (Safety and Isolation)
-
One process must not access or modify another process’s memory.
-
Programs must also be prevented from damaging the OS memory.
-
This ensures isolation: if one process crashes or behaves badly, others remain safe.
-
Protection improves system reliability and security.
In Short
Virtual memory has three key goals:
-
Transparency – programs see a private memory space.
-
Efficiency – memory virtualization is fast and resource-efficient.
-
Protection – processes and the OS are isolated from one another.
๐ Summary
Virtual memory is the technique by which the OS makes every process believe it has its own large private memory, while actually mapping those virtual addresses to shared physical memory behind the scenes.



Comments
Post a Comment