Difference between swapping and paging


Welcome to a new operating systems post. Today, we are going to clarify the difference between swapping and paging. The goal is to focus on the main difference between the two and see why is it sometimes confusing. Let us give it a try…

Modern operating systems support multitasking and virtual memory. I recommend that you read (at least skim through) the following articles to get a better context:

  1. Multiprogramming, multitasking, multithreading and multiprocessing
  2. Virtual memory, paging and segmentation
  3. Page tables vs inverted page tables
  4. Page replacement algorithms

Now that you have a better idea what virtual memory means, let us see why swapping can be confused with paging or visa versa...

  1. Not realizing what operating system we are talking about. Roughly speaking, swapping is an old operating system concept while paging is a modern operating system concept. Note that swapping can also be used in modern operating systems. Will see how later...
  2. Referring to swap area or swap device in the context of paging
  3. Using the term swapping a page instead of page replacement

Let us elaborate on the points above. What terminology to use is not something written on stone. Swapping and paging can be used interchangeably but we need to be aware of what we are talking about otherwise, confusion may happen...

  1. Old vs modern operating systems: historically speaking, old operating systems (ex. multiprogramming systems) used to load entire programs into memory for execution by the CPU. A scheduling algorithm decides which process uses the CPU next. If main memory cannot fit all processes, the operating system evicts a process out of memory back to disk (secondary storage). This technique is called swapping. On the other hand, modern operating systems use paging technique. Instead of loading the entire program into memory, only the needed portions are loaded. The operating system divides the program into equally sized blocks called pages. Similarly, instead of evicting the entire program, only individual pages are removed from main memory. This page in, page out technique is called paging
  2. Swap area or swap device: some operating systems do support both paging and swapping. If that is the case, we need to be aware of what we are referring to. Swapping in this case occurs when the operating system decides to evict all pages of a given process if memory is low (i.e. heavy load) which rarely happens. We should distinguish this scenario from regular paging activity
  3. In paging based operating system, if a page has to be removed from memory, we should use the term page replacement instead of swapping the page. There is nothing wrong linguistically with the word swap but using swapping instead of page replacement can also lead to confusion

In short, swapping refers to evicting entire process from memory (to disk) in case of old operating systems (ex. multiprogramming os) or removing all pages of that process in case of modern operating system. On the other hand, paging refers to removing (in case of page out as paging can also mean loading a page from disk) individual pages of a given process.

Iterating what is paging and swapping...

What is paging?

User applications are loaded into memory for execution. They can be too big to efficiently fit in RAM therefore, modern operating systems implement virtual memory. Programs are divided into smaller blocks called pages and loaded on demand. If the requested page does not exist in memory, a page fault occurs. As a result, the requested page is retrieved (page in) from secondary storage (i.e. disk). Sometimes, the operating system decides to remove (page out) a page from main memory. In this case, it is better to say the page was replaced instead of swapped otherwise it can be confused with process swapping.

What is swapping?

If memory is full (i.e. high load) swapping moves entire process address space (ex. multiprogramming systems) or all process pages (ex. OS supports both paging and swapping) from main memory back to disk (swap device). Disk area (swap space) is used as an extension of physical memory for temporary storage so that it can be brought back to memory to resume execution.

In the next section, we will briefly mention few concepts related to swapping and paging...

Related topics

  • Dirty page: a page that has been modified in memory is called a dirty page. Before evicting a dirty page, the operating system preserves its content by saving it to the swap file on disk so that it can be accessed later after being removed from memory. Saving pages to disk and retrieving pages back is a time consuming process. Special algorithms such as LRU are used to manage what pages to keep in memory
  • LRU: Linux uses the least recently used algorithm to decide which pages to keep and which pages to remove. LRU assigns an age to individual pages. The more the page is accessed the younger it is therefore it stays longer and visa versa
  • Thrashing: If the operating system ends up doing more paging activity due to low memory instead of running applications. This state is called thrashing which is not a good sign but it rarely happens
  • Fragmentation: paging is a flexible virtual memory technique which helps in mitigating external fragmentation. External fragmentation is a problem that old multiprogramming systems suffer from when using swapping. It refers to unmanaged free space in memory that cannot be used to load more processes
  • Paging activity: on Linux, you can use the vmstat and top commands to check virtual memory activity and statistics

Thanks for reading. Please use the comments section below for feedback

Leave a Reply