Memory Organization
Seven topics explained step by step, with pictures, flowcharts, tables and solved examples.
1Memory Hierarchy
No single memory is fast, big and cheap at the same time. So a computer uses several kinds of memory in layers, like a pyramid. The top is tiny and very fast. The bottom is huge and slow.
Volatile memory loses data when power is off: registers, cache and RAM. Non-volatile memory keeps data: SSD, hard disk, optical disc and tape.
Why does the pyramid work? Locality of reference
2Main Memory
Main memory holds the programs and data the CPU is using right now. The CPU can address it directly. It is made of RAM (read and write, loses data without power) and ROM (mostly read-only, keeps data).
| Feature | SRAM | DRAM |
|---|---|---|
| Cell | 6 transistors (a latch) | 1 transistor + 1 capacitor |
| Refresh | Not needed | About every 64 ms |
| Speed | Faster | Slower |
| Cost and density | Costly, low density | Cheap, high density |
| Used for | Cache, registers | Main memory |
DDR SDRAM sends data on both clock edges, so one clock moves two words. DRAM sends the row address first (RAS) and then the column address (CAS), which halves the address pins.
ROM types
| Type | Programmed | Erased |
|---|---|---|
| Mask ROM | At the factory | Never |
| PROM | Once, by the user | Never |
| EPROM | By the user | UV light, whole chip |
| EEPROM | By the user | Electrically, byte by byte |
| Flash | By the user | Electrically, in blocks (USB, SSD) |
Address 0x9A3C has A15 A14 = 10, so it is in chip 2.
3Secondary Memory
Secondary memory is permanent bulk storage. The CPU does not run programs straight from it. Data is copied in blocks into main memory.
Magnetic disk
- Platters spin on a spindle (5400 or 7200 rpm). Each surface has a head on a shared arm.
- A track is split into sectors (512 bytes or 4 KB).
- Access time = seek time + rotational latency + transfer time.
Average rotational latency = 0.5 × 60 ÷ rpm. Transfer time = rotation time ÷ sectors per track.
Other devices
| Disc | Laser | Capacity (one layer) |
|---|---|---|
| CD | 780 nm infrared | about 700 MB |
| DVD | 650 nm red | 4.7 GB |
| Blu-ray | 405 nm blue-violet | 25 GB |
4Cache Memory
A cache is a small, very fast SRAM between the CPU and main memory. It keeps copies of recently used blocks. If the word is found, it is a hit. If not, it is a miss and the whole block is fetched from memory. Think of it as notes kept on your desk instead of walking to the library each time.
Each cache line stores a valid bit, a tag, the data block (and a dirty bit in write-back caches). The address is split into three parts:
compared with stored tag
picks the line
picks the word
Cache read flowchart
| Write policy | What happens | Trade-off |
|---|---|---|
| Write-through | Write cache and memory together | Simple, memory always current, slower |
| Write-back | Write cache only, set dirty bit, write memory on eviction | Less bus traffic, needs dirty bits |
Mapping: where can a block go?
Example: 64K-word memory (16-bit address), 1K-word cache, 16-word blocks, so 1024 ÷ 16 = 64 lines.
| Scheme | Block can go to | Address split | Conflict misses |
|---|---|---|---|
| Direct | 1 line (block mod 64) | tag 6, index 6, offset 4 | Highest |
| Fully associative | Any line | tag 12, offset 4 | None |
| 4-way set associative | Any of 4 lines in one set (16 sets) | tag 8, set 4, offset 4 | Medium |
Replacement and speed-ups
Ways to improve a cache: multi-level cache (L2 answers L1 misses), bigger cache (fewer capacity misses), bigger blocks (use spatial locality), more associativity (fewer conflict misses), victim cache, prefetching, write buffer and non-blocking cache. Direct mapping needs no replacement policy.
5Virtual Memory
Virtual memory gives each program its own large, private address space, even when RAM is small and shared. Only the pages in use stay in RAM. The rest wait in the swap area on disk.
Paging
Virtual memory is cut into equal pages. RAM is cut into frames of the same size (often 4 KB). The page table maps each virtual page number (VPN) to a frame number. Each entry has a valid bit, protection bits, and dirty and reference bits.
Page table size: 32-bit address, 4 KB pages, 4-byte entries. VPN = 20 bits, so 220 entries × 4 B = 4 MB per process. Multi-level and inverted page tables reduce this.
TLB: a cache for translations
The translation lookaside buffer (TLB) is a small, fast cache of recent VPN-to-frame translations. Without it, every access needs two memory reads.
Page fault handling
- The MMU sees valid = 0 and traps to the OS.
- The OS checks the reference is legal, otherwise it ends the process.
- The OS finds a free frame, or picks a victim (saved to disk first if dirty).
- The OS reads the needed page from disk into the frame.
- The OS updates the page table and TLB (valid = 1).
- The faulting instruction restarts.
Page replacement
6Memory Management Hardware
The MMU sits between the CPU and memory. It translates every address in hardware and protects memory, so one process cannot touch another process or the OS. An illegal access becomes a trap to the OS.
Base 30000, limit 12000: logical 500 gives 30500. Logical 13000 traps.
72,766 B, 2048-byte pages: 36 pages, waste = 2048 − 1086 = 962 B.
Segment 2: base 4300, limit 400. (2, 53) gives 4353. (2, 500) traps.
Contiguous allocation (base and limit) suffers from external fragmentation. Segmented paging splits each segment into pages to keep a logical structure without external fragmentation.
| Point | Paging | Segmentation |
|---|---|---|
| Unit | Fixed-size page | Variable-size segment |
| Address | Page number + offset | Segment number + offset |
| Fragmentation | Internal | External |
| Protection and sharing | Per page | Per segment, more natural |
| Visible to programmer | No | Yes |
Other hardware helpers
7Formula Sheet and Exam Questions
| Topic | Formula or fact |
|---|---|
| Address lines | 2n locations need n address bits. Chips = total capacity ÷ chip capacity |
| Disk capacity | surfaces × tracks × sectors per track × bytes per sector |
| Disk access time | seek + 0.5 × (60 ÷ rpm) + (60 ÷ rpm) ÷ sectors per track |
| Cache lines | cache size ÷ block size |
| Cache address bits | offset = log2(block size), index = log2(lines or sets), tag = the rest |
| Sets | lines ÷ ways (direct: 1 way, fully associative: 1 set) |
| AMAT | hit time + miss rate × miss penalty |
| Two-level AMAT | H1 + M1 × (H2 + M2 × memory) |
| Page translation | frame × page size + offset |
| Page table size | 2(VPN bits) × entry size |
| TLB EAT | h × (tTLB + tmem) + (1 − h) × (tTLB + 2 × tmem) |
| Page-fault EAT | (1 − p) × tmem + p × tfault |
| Base and limit | address < limit, then base + address, else trap |
| Segmentation | d < limit[s], then base[s] + d, else trap |
Likely exam questions
- Compare SRAM and DRAM, and list the ROM types with their erase methods.
- Draw the memory hierarchy and explain why it works (locality).
- Calculate disk capacity and average access time from the given geometry.
- Explain direct, associative and set-associative mapping with the address split, and find hits and misses for a block sequence.
- Compare LRU, FIFO and Optimal on a reference string and count the page faults.
- Explain how a virtual address becomes a physical address, including the TLB and a page fault.
- Compare paging and segmentation, and explain base and limit protection.