Skip to content

Memory Systems

A memory hierarchy is a layered organization of storage according to access time, capacity, cost per bit, volatility and proximity to the CPU. No one technology is simultaneously fastest, largest, cheapest and non-volatile, so active information is copied into small fast levels while bulk information remains in larger slow levels.

The physical memory hierarchy. Virtual memory is a separate address-translation and protection mechanism that may use backing storage.

The physical memory hierarchy. Virtual memory is a separate address-translation and protection mechanism that may use backing storage.

LevelTypical propertyMain function
CPU registersFastest, only a few bytes or words, volatileHold the current instruction, addresses, operands and results
Cache (L1/L2/L3)Very fast SRAM, small and volatileKeep copies of recently or nearby used memory blocks
Main memoryDRAM, typically gigabytes, volatileHold active programs and data
Auxiliary storageLarge non-volatile SSD, HDD, optical disk or tapeStore programs, files and archives persistently

Levels of the memory hierarchy.

Virtual memory is not another physical storage tier between RAM and disk. It is an address-translation, isolation and protection mechanism: virtual pages map to RAM frames, and selected nonresident pages may have copies in backing storage. A system can use virtual addressing even when it does not page data to disk.

Moving downward in the figure increases capacity and access time while reducing cost per bit. Moving upward increases speed and cost per bit. Registers and caches are managed mainly by compiler and hardware, physical-memory allocation and virtual mappings by the memory-management unit (MMU) plus the operating system, and files by the operating system and applications.

The hierarchy works because normal programs do not reference all addresses uniformly:

  • Temporal locality: a recently referenced instruction or datum is likely to be referenced again soon, as in a loop variable.

  • Spatial locality: addresses near a referenced item are likely to be used soon, as in sequential array traversal.

  • Sequential access: instruction execution normally proceeds through neighboring addresses until a branch.

Information moves between adjacent levels in blocks: a cache line between cache and RAM, a page between RAM and backing storage, and a file-system block between secondary storage and memory. Larger blocks exploit spatial locality but increase transfer time and may fetch unused bytes.

A cache is a small, fast semiconductor memory logically between the CPU and main memory. It stores copies of selected instruction and data blocks so that most references avoid the longer DRAM access time.

Typical systems use a small private L1 instruction cache and L1 data cache, a larger L2 cache and a still larger shared L3 cache. On a hit, a valid entry has a tag matching the requested address and the selected word is returned. On a miss, the next hierarchy level supplies a complete line; the requested word is forwarded and the line is installed for reuse.

Cache lookup, tag test, hit return and miss-refill paths. The address is split into tag, index and offset fields, and locality makes hits common.

Cache lookup, tag test, hit return and miss-refill paths. The address is split into tag, index and offset fields, and locality makes hits common.

As the figure shows, an address is interpreted as tag  index  offset. The index selects a line or set, the tag identifies which main-memory block occupies it, and the offset selects the byte or word within the line. A matching tag without a set valid bit is not a hit.

MappingPlacement ruleMain trade-off
Direct mappedEach memory block has exactly one possible cache lineFast, simple and inexpensive, but vulnerable to repeated conflicts
Fully associativeA block may occupy any cache lineEliminates placement conflicts, but requires costly parallel tag search and replacement choice
Set associativeA block selects one set and may occupy any way in that setPractical balance among hit time, hardware cost and conflict misses

Cache mapping methods.

If HH is hit ratio, TcT_c is cache lookup time and TmT_m is the additional main-memory time on a miss, then

H=number of cache hitstotal memory accesses,Tavg=HTc+(1−H)(Tc+Tm)=Tc+(1−H)Tm.\begin{aligned} H&=\frac{\text{number of cache hits}}{\text{total memory accesses}},\\ T_{avg}&=H T_c+(1-H)(T_c+T_m)\\ &=\boxed{T_c+(1-H)T_m}. \end{aligned}

The equivalent general notation is

AMAT=Thit+rmissPmiss,\boxed{\mathrm{AMAT}=T_{hit}+r_{miss}P_{miss}},

where rmiss=1−Hr_{miss}=1-H and PmissP_{miss} is the additional miss penalty. Thus, for Thit=1 nsT_{hit}=1\,\text{ns}, rmiss=0.02r_{miss}=0.02 and Pmiss=50 nsP_{miss}=50\,\text{ns},

AMAT=1+0.02(50)=2 ns.\mathrm{AMAT}=1+0.02(50)=\boxed{2\,\text{ns}}.
MissCauseTypical mitigation
Compulsory (cold)First access to a block; no prior copy can be presentPrefetching or a larger line when spatial locality is strong
CapacityActive working set is larger than the cacheLarger cache or a smaller/reorganized working set
ConflictMapping forces simultaneously useful blocks into the same line or set despite free space elsewhereMore associativity or different placement

The three classical cache-miss causes.

Random access weakens spatial locality; a workload larger than the cache causes capacity misses. In multicore systems, coherence actions can invalidate a line and create additional coherence traffic or misses.

PolicyOperationConsequence
Write-throughUpdate cache and next memory level on every hitSimple memory consistency, but high write traffic; a write buffer is normally used
Write-backUpdate cache only and mark the line dirty; write it to the next level when evictedLower traffic, but needs dirty bits and more complex replacement/coherence handling
Write-allocateOn a write miss, fetch the line then write in cacheExploits future locality; commonly paired with write-back
No-write-allocateOn a write miss, send the write downward without filling the cacheAvoids filling with data unlikely to be reused; often paired with write-through

Cache write-policy choices.

Random-access memory (RAM) permits direct read/write access to any address but is volatile. Read-only memory (ROM) and its programmable variants retain contents without power and are used for firmware or fixed data.

TypeStorage and refreshRelative propertyMain use
SRAMBistable cell; no periodic refresh while poweredFaster, costlier and lower densityProcessor cache
DRAMCapacitor charge; must be periodically refreshedSlower, cheaper and higher densityMain memory

SRAM and DRAM comparison.

VariantDefining feature
PROMUser-programmable once; it cannot normally be erased
EPROMErased by ultraviolet light and then reprogrammed
EEPROMElectrically erased and rewritten, commonly at byte granularity
Flash memoryElectrically erased in blocks, giving high density for firmware and solid-state storage

ROM and non-volatile semiconductor variants.

StorageCharacteristicsTypical use
HDDMagnetic rotating disk with mechanical seek and low cost per gigabyteLarge economical online storage
SSDFlash based, with no moving parts and low random-access latency; cells have finite write enduranceOperating system, applications and active data
Optical diskLaser-read or laser-written removable mediumCD, DVD and Blu-ray distribution or archive
Magnetic tapeVery high capacity and low cost, but primarily sequential accessBackup and long-term archive

Common secondary-storage technologies.

PropertyCacheMain memoryAuxiliary storage
TechnologySRAMDRAMFlash or magnetic/optical media
CPU accessHardware tag lookupThrough memory controllerThrough I/O and the operating system
Unit movedCache lineByte/word at the interfaceBlock or file
PersistenceNoNoYes
Main limitCapacity, misses and coherence costRefresh and finite capacityHigh latency, wear or mechanical delay

Cache, main memory and auxiliary storage.

Virtual memory is a hardware-and-operating-system mechanism that gives each process a large private logical address space independent of the amount and placement of physical RAM. With paging, virtual memory is divided into equal-size pages and RAM into frames of the same size.

A virtual address is divided into

virtual page number (VPN)∣page offset.\boxed{\text{virtual page number (VPN)}\mid\text{page offset}}.

The MMU looks for the VPN in a translation lookaside buffer (TLB), a small cache of recent address translations. On a TLB hit, its frame number is combined with the unchanged offset. On a TLB miss, hardware or operating-system software reads the process page table. A TLB miss is therefore not necessarily a page fault: the page-table entry may show that the page is already in RAM.

FieldPurpose
Frame numberIdentifies the RAM frame containing a present page
Present/valid bitDistinguishes a resident legal translation from an absent page; architectures may use separate bits for validity and presence
Protection bitsPermit or forbid read, write, execute and user-mode access
Referenced/accessedRecords recent use for page-replacement decisions
Dirty/modified bitRecords a write so an evicted page can be saved first
Backing-store slotIdentifies where an absent page can be reloaded

Essential page-table-entry fields.

For byte addressing and page size SS bytes, the physical address is

PA=(frame number×S)+offset.\boxed{\mathrm{PA}=(\text{frame number}\times S)+\text{offset}}.

Virtual-memory translation through the TLB and page table. An illegal or forbidden access raises a protection fault; a legal but nonresident VPN 1 causes a page-fault reload from disk slot S17 into physical frame 2.

Virtual-memory translation through the TLB and page table. An illegal or forbidden access raises a protection fault; a legal but nonresident VPN 1 causes a page-fault reload from disk slot S17 into physical frame 2.

If a referenced legal page is absent, the page fault in the figure traps to the operating system:

  1. Save enough process state to restart the faulting instruction and verify that the virtual address and requested access are legal.

  2. Obtain a free frame or choose a victim using the replacement policy. If the victim is dirty, write it to backing storage before reuse.

  3. Read the required page from SSD or disk into the selected frame. The process waits while another process may run.

  4. Update the page table, repair or invalidate the affected TLB entry, and restart the faulting instruction so translation is retried.

An illegal address or a forbidden read/write/execute access causes a protection exception and normally a process signal or termination; it must not be treated as an ordinary demand-page reload. Storage service is millions of CPU cycles slower than a cache or RAM access, so even a low page-fault rate can dominate execution time.

Paging allows a program to use an address space larger than currently assigned RAM, relocates pages without changing program addresses, isolates processes, supports multiprogramming, shares selected pages and applies per-page protection. Its costs include page-table memory, TLB/page-walk overhead, internal fragmentation in partly used final pages and extremely slow faults.

Thrashing occurs when the active working sets of running processes do not fit in available frames. The system repeatedly faults and replaces pages that are needed again almost immediately, so storage traffic rises and useful CPU work collapses. Remedies include allocating more frames, reducing the degree of multiprogramming, using locality-aware replacement and controlling each process by its working set.

  • Hierarchy: registers →\rightarrow cache →\rightarrow RAM →\rightarrow secondary storage trades speed for capacity and cost.

  • Cache: uses temporal and spatial locality; a hit returns a matching valid line, while a miss fetches and installs a block.

  • AMAT: Thit+rmissPmissT_{hit}+r_{miss}P_{miss}; always identify whether the stated penalty is additional.

  • Paging: maps VPNs to equal-size physical frames; the offset is unchanged.

  • TLB miss versus page fault: a TLB miss requests a page-table lookup; a page fault means the legal page is absent from RAM.