# Swapping and Contiguous Memory Allocation

> Swapping is the technique of temporarily moving an entire process out of RAM to disk to free up space for another process. Contiguous memory allocation gives each process a single unbroken block of physical memory, defined by a base address and a size, which is simple and fast to translate but leads to fragmentation over time.

Canonical: https://devsunite.com/resources/core-subjects/operating-systems/swapping-and-contiguous-memory-allocation
Last updated: 2026-11-22

Subject: Operating Systems · Category: Memory Management · 4 min read

Before paging and virtual memory became standard, operating systems managed memory with two closely related ideas: contiguous allocation (giving each process one continuous block of physical RAM) and swapping (temporarily moving a whole process out to disk when RAM is full, to make room for something else).

## Contiguous memory allocation

In contiguous allocation, each process is loaded into a single, unbroken range of physical memory addresses, one base address and one size. This is simple to implement and fast to translate (as covered in the logical vs physical addressing topic, translation is just base + offset). The tradeoff is that memory gets carved up into blocks over time as processes come and go, leading to fragmentation (its own dedicated topic covers this in depth): free memory ends up scattered in small unusable gaps between allocated blocks, even when the total free memory would technically be enough for a new process, if only it were contiguous.

Allocation strategies for picking which free block to use for a new process include first-fit (use the first free block big enough), best-fit (use the smallest free block that's still big enough, minimizing wasted space), and worst-fit (use the largest available block, leaving a bigger leftover chunk that might be more useful later).

## Swapping

Swapping is a separate but related technique: when physical memory is full and a new or waiting process needs to run, the OS can move an entire process's memory image out to a reserved area of disk (called the swap space or backing store), freeing up that RAM for something else. Later, when the swapped-out process needs to run again, its entire memory image is swapped back into RAM, sometimes into a different physical location than before (which is exactly why the logical/physical address translation matters, the process doesn't need to know or care where it ends up).

## A worked example

Say physical memory has three processes loaded contiguously: P1 (0-99), P2 (100-249), and a free gap from 250-399, then P3 (400-499).

A new process, P4, needs 200 units of contiguous memory. First-fit would scan from the start and find the first block big enough, here the 250-399 gap is only 150 units, not big enough, so first-fit continues and might swap out a process to make room, or reject the request if nothing else fits either.

If instead the OS decides to swap P2 out to disk to make room, physical memory now has P1 (0-99), a much larger free block from 100-399 (250 units, since P2's old space is now also free), and P3 (400-499). P4 can now be loaded contiguously into that combined space.

## Where this matters in real systems

Pure contiguous allocation and full-process swapping are largely historical at this point; virtually every modern general-purpose OS uses paging (its own topic) combined with virtual memory instead, which avoids most of contiguous allocation's fragmentation problems by breaking memory into fixed-size chunks that don't need to be adjacent. That said, the core idea of swapping survives directly in the form of swap space on Linux and the page file on Windows, both of which still move memory contents out to disk under pressure, just at the granularity of individual pages rather than whole processes.

## What interviewers actually ask about swapping and contiguous allocation

- **"What is contiguous memory allocation?"** Giving each process a single unbroken range of physical memory addresses.
- **"What problem does contiguous allocation create over time?"** Fragmentation, free memory scattered into gaps too small individually to satisfy new requests even though the total free space might be enough.
- **"What is swapping?"** Temporarily moving an entire process's memory image out to disk to free RAM, then bringing it back later, potentially to a different physical location.
- **"How does modern virtual memory relate to swapping?"** It generalizes the same idea to individual pages instead of whole processes, which is why Linux still calls its equivalent mechanism "swap space."

## Key takeaways

- Contiguous allocation gives each process one continuous block of physical memory, simple but prone to fragmentation.
- First-fit, best-fit, and worst-fit are the standard strategies for choosing which free block to use.
- Swapping moves an entire process out to disk to free RAM, and back in later, possibly to a different location.
- Modern systems generalize this idea via paging and virtual memory, operating at page granularity instead of whole processes.

## Related

- [Previous: Logical vs Physical Address Space](https://devsunite.com/resources/core-subjects/operating-systems/logical-vs-physical-address-space)
- [Next: Fragmentation in Operating Systems](https://devsunite.com/resources/core-subjects/operating-systems/fragmentation-in-os)
- [More in Memory Management: Paging in Operating Systems](https://devsunite.com/resources/core-subjects/operating-systems/paging-in-os)
- [More in Memory Management: Segmentation in Operating Systems](https://devsunite.com/resources/core-subjects/operating-systems/segmentation-in-os)
- [More in Memory Management: Virtual Memory and Demand Paging](https://devsunite.com/resources/core-subjects/operating-systems/virtual-memory-and-demand-paging)
- [More in Memory Management: Page Fault Handling](https://devsunite.com/resources/core-subjects/operating-systems/page-fault-handling)
- [More in Memory Management: Page Replacement Algorithms](https://devsunite.com/resources/core-subjects/operating-systems/page-replacement-algorithms)
- [All 205 OS interview questions](https://devsunite.com/resources/core-subjects/operating-systems)
