1. Home
  2. Explore
  3. Computer Science
  4. Memory Management Overview

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Memory Management Overview

Memory Management in Operating Systems

Introduction to Memory Management

One of the key tasks of a modern operating system is to allow multiple applications to run concurrently. Although a single CPU can only execute one instruction at a time, rapid context switching gives the illusion of parallelism. All active processes must be resident in main memory because the CPU can directly access only main memory and its registers — programs cannot run from disk or other storage. This creates a need for careful memory management to prevent processes from interfering with each other and to ensure efficient use of limited memory.

Several fundamental constraints drive memory management design:

  • The CPU only understands addresses — it does not distinguish between data, instructions, or other content.

  • Memory access is significantly slower than register access. Register access takes 1 clock cycle, while main memory access requires many cycles (the CPU stalls while waiting).

  • Caches can be placed between main memory and the CPU to reduce this speed disparity.

Hardware Address Protection

A simple method to protect memory is to use a base register and a limit register for each process. The base register holds the starting physical address of the process; the limit register holds the size of the process (the number of bytes it may access). Every memory access generated by the process is checked by hardware:

  • Is the requested address ≥ the base address?

  • Is the requested address < base + limit?

If either check fails, an error is generated and the process is typically terminated. This scheme is called hardware address protection — it is built into the CPU with dedicated registers and comparison logic, ensuring fast checks without software intervention.

Binding of Instructions and Data

When a program is written, it uses symbolic addresses (e.g., variable names). These must be bound to actual physical memory addresses before execution. Binding can occur at different times:

  • Compile time: If the memory location is known in advance (e.g., in embedded systems), absolute code can be generated.

  • Load time: If the program will be loaded into variable memory locations, the compiler generates relocatable code. The loader performs address binding when the program is loaded.

  • Execution time: If a process can be moved around in memory during execution (e.g., due to swapping), binding must be delayed until runtime. This requires specialized hardware support.

Most general-purpose operating systems use load-time or execution-time binding, as the final memory location is not known until the program is loaded.

Logical vs Physical Address Space

The logical address is the address as seen by the process (starting from 0). The physical address is the actual location in main memory. The CPU generates logical addresses, which are translated to physical addresses by a hardware device called the Memory Management Unit (MMU).

  • The logical address space is the range of addresses a process can generate.

  • The physical address space is the range of addresses available in main memory.

In the simplest MMU scheme, the base register is called a relocation register. When the CPU sends a logical address to the MMU, the MMU adds the value in the relocation register to produce the physical address. The process never knows its true physical location.

Definition: The MMU is hardware that maps logical addresses to physical addresses at runtime, enabling protection and relocation without modifying the process code.

Dynamic Linking

Instead of linking all libraries into the executable at compile time (static linking), many systems use dynamic linking. A dynamically linked library (DLL) is only loaded into memory when its functions are actually called. This saves memory and reduces load time.

  • Shared libraries (e.g., the spell-check DLL used by Word, Excel, and PowerPoint) can be loaded once and shared among multiple processes. The operating system maintains a stub for each DLL reference — when the process calls the DLL, the stub checks if the library is already in memory; if not, it loads it and updates the address.

  • Dynamic linking requires relocatable load format and careful management of address space identifiers.

  • A common issue is version conflicts: different programs may require different versions of the same DLL, leading to errors like "wrong version of DLL."

Swapping

If a process is idle, it can be swapped out to backing storage (usually disk) to free up main memory for other processes. Later, when the process needs to run again, it is swapped back in. Because the process may be placed at a different physical address when swapped back, execution-time binding is required.

  • Swapping allows the degree of multiprogramming to exceed the capacity of main memory.

  • Backing storage must be fast (e.g., SSDs reduce swap times significantly compared to HDDs).

  • The time to swap a process in/out can be large, so CPU scheduling must account for this — a process waiting to be swapped in may need prioritization.

  • Mobile operating systems (e.g., iOS, Android) often do not use swapping due to limited write endurance in flash storage; instead they may terminate and restore processes with saved state.

Contiguous Memory Allocation

In contiguous memory allocation, each process occupies a single contiguous block of memory. The operating system keeps track of which parts of memory are free (holes) and which are occupied.

  • Multiple-partition scheme: After the OS is loaded, the remaining memory is available for processes. When a process terminates, a hole appears.

  • Dynamic storage allocation problem: Choosing which free hole to allocate for a new process. Common strategies:

First-Fit

Allocate the first hole that is large enough. Simple and fast.

Best-Fit

Allocate the smallest hole that is large enough. Aims to minimize wasted space, but tends to produce many tiny holes.

Worst-Fit

Allocate the largest hole, leaving the biggest leftover. The leftover is more likely to be useful for another process, but in practice often performs no better than first-fit.

None of these strategies are perfect; modern operating systems typically avoid contiguous allocation entirely.

Fragmentation

Contiguous allocation inevitably leads to fragmentation.

  • External fragmentation: There is enough total free memory for a new process, but it is scattered in small holes — not contiguous.

  • Internal fragmentation: Allocated memory may be slightly larger than needed (e.g., due to fixed partition sizes), wasting space inside a block.

Analysis shows that with dynamic allocation, up to half of memory can be lost to fragmentation. One solution is compaction — shuffling processes together to combine free holes into one large block. However, compaction is time-consuming and requires dynamic relocation support.

  • Defragmentation is the analogous process for hard disks; modern systems often perform it automatically.

Segmentation

Instead of treating a process as one monolithic block, segmentation divides the process into logical units (e.g., code, data, stack, shared libraries). Each segment has its own base and limit, and a segment table in memory maps segment numbers to physical base addresses and lengths.

  • The logical address is a pair: (segment number, offset).

  • The segment table is stored in memory; one register points to its base, another to its length.

  • Protection can be per-segment (e.g., read-only, read-write).

  • Segmentation still results in external fragmentation because segments are variable-sized.

Paging

Paging solves external fragmentation by dividing both logical memory (the process) and physical memory into fixed-size blocks. The process's logical memory is split into pages; physical memory is split into frames, each the same size as a page. A page table maps every logical page number to a physical frame number.

  • No external fragmentation: any free frame can hold any page.

  • Internal fragmentation is still possible: the last page of a process is rarely completely full. On average, half a page per process is wasted.

  • The page size is a design trade-off: smaller pages reduce internal fragmentation but increase the size of the page table and overhead.

Free Frame List

The OS maintains a list of free frames. When a process with N pages is loaded, N frames are allocated from the list. Because frames are all the same size, allocation order is irrelevant — no need for sorting or complex selection.

Page Table Implementation

The page table is stored in main memory. A page-table base register (PTBR) points to the page table; a page-table length register (PTLR) holds its size. Every memory access now requires two physical accesses: one to fetch the page-table entry, and one to access the actual data. This doubles the memory access time.

Translation Lookaside Buffer (TLB)

To speed up paging, a fast associative memory called the Translation Lookaside Buffer (TLB) is used. The TLB caches recent page-table entries and can perform a parallel search — all entries are compared simultaneously. If the page number is found in the TLB (hit), the corresponding frame number is returned immediately (one memory access only). If not (miss), the page table in memory must be consulted (two memory accesses total).

  • The TLB typically contains address space identifiers (ASIDs) to distinguish entries from different processes. On context switch, the TLB must be flushed or tagged.

  • The effective access time (EAT) can be calculated:

Let

  • α\alphaα = hit ratio (fraction of TLB hits)

  • ϵ\epsilonϵ = TLB search time

  • mmm = memory access time

Then

  • Hit cost = ϵ+m\epsilon + mϵ+m

  • Miss cost = ϵ+m+m=ϵ+2m\epsilon + m + m = \epsilon + 2mϵ+m+m=ϵ+2m

EAT=α(ϵ+m)+(1−α)(ϵ+2m)EAT = \alpha (\epsilon + m) + (1 - \alpha)(\epsilon + 2m)EAT=α(ϵ+m)+(1−α)(ϵ+2m)

Example with ϵ=20 ns\epsilon = 20 \, \text{ns}ϵ=20ns, m=100 nsm = 100 \, \text{ns}m=100ns:

  • α=0.80\alpha = 0.80α=0.80: EAT=0.8×120+0.2×220=140 nsEAT = 0.8 \times 120 + 0.2 \times 220 = 140 \, \text{ns}EAT=0.8×120+0.2×220=140ns (a 40% slowdown)

  • α=0.99\alpha = 0.99α=0.99: EAT=0.99×120+0.01×220=121 nsEAT = 0.99 \times 120 + 0.01 \times 220 = 121 \, \text{ns}EAT=0.99×120+0.01×220=121ns (only 21% slowdown)

High hit ratios (≈99%) are achieved with careful TLB management and locality of reference.

Protection in Paging

Each page-table entry can include protection bits (read, write, execute) and a valid–invalid bit. A valid entry indicates the page is in main memory; an invalid entry indicates the page is not present (e.g., on disk or does not belong to the process). An access to an invalid page generates a trap.

Shared Pages

Reentrant code (code that does not modify itself) can be shared among multiple processes. For example, the same spell-check code can be mapped into the logical address spaces of several processes, while each process has its own private data pages. This reduces memory usage. The page tables of different processes can point to the same frames for shared code.

Summary

Concept

Key Idea

Base/Limit Registers

Simple hardware protection for contiguous processes

Binding

Compile time, load time, execution time

MMU

Translates logical to physical addresses

Dynamic Linking

Load libraries on demand, share code

Swapping

Move entire processes to backing store

Contiguous Allocation

Holes, first-fit/best-fit/worst-fit

Fragmentation

External (holes) and internal (wasted space)

Compaction

Shuffle memory to combine free space

Segmentation

Variable-sized logical segments, still fragmented

Paging

Fixed-size pages/frames, no external fragmentation, internal waste ~½ page

TLB

Fast associative cache for page table, reduces access time

Shared Pages

Reentrant code mapped into multiple process address spaces

Paging is the foundation of most modern memory management systems because it eliminates external fragmentation, simplifies allocation, and efficiently supports sharing and protection. The overhead of page-table lookups is mitigated by TLBs, and the internal fragmentation waste is acceptable given the typical page size (e.g., 4 KB).

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent