What is the Difference Between Swapping and Paging

The main difference between swapping and paging is that, in swapping, the processes move back and forth between the main memory and secondary memory while, in paging, equal size memory blocks, called pages, move between the main memory and the secondary memory.

An operating system handles the main functionalities of the computer system. It handles hardware devices, performs process scheduling, file handling, and many other tasks. Among them, one is memory management. It keeps track of all memory locations and status of whether they are allocated or free. Two memory management techniques of an operating system are swapping and paging.

Key Areas Covered

1. What is Swapping
     – Definition, Functionality
2. What is Paging
     – Definition, Functionality
3. What is the Difference Between Swapping and Paging
    – Comparison of Key Differences

Key Terms

Logical Address, Main Memory, Paging, Physical Address, Swapping, Virtual Memory

Difference Between Swapping and Paging - Comparison Summary

What is Swapping

Swapping is the mechanism of moving the processes into the main memory from the secondary memory and moving the processes out of the main memory back to the secondary memory. For example; the programs are initially in the hard disk or secondary memory. When it is necessary to execute them, these programs move into the main memory. The CPU can access the programs faster in the main memory than in the secondary memory.

After completing the execution, the programs go back to the hard disk. Here, the memory allocated to the executing programs gets freed after completing the execution. Then, a new program can use this freed memory. A program in execution is also called a process. Even though swapping affects the performance, it allows running multiple processes in parallel. Therefore, it is also a technique for memory compaction.

What is Paging

Virtual memory is a type of memory that allows the programmers to use more memory for a program than the available main memory. For example, if the main memory is 8GB and the virtual memory is 16GB, the programmer can use that 16GB virtual memory to execute the program. Here, the paging technique helps to implement virtual memory. It divides the process address space into same size blocks called pages. Similarly, it divides the main memory into small fixed-sized blocks called frames.

Difference Between Swapping and Paging

A page address is called a logical address. The equation is as follows.

Logical address = Page number + page offset

The frame address is called the physical address. The equation is as follows.

Physical address = Frame number + page offset

When allocating a frame to a page, the logical address translates into a physical address. Moreover, the page map table keeps records on the pages of processes and the corresponding main memory frames throughout the execution of the program.

When executing a process, the corresponding pages of that process loads into any available frame in the main memory.  When the computer runs out of main memory, the OS moves the unwanted pages to secondary memory and free up the main memory for other processes. This process continues throughout the execution of the program.

Difference Between Swapping and Paging

Definition

Swapping is a mechanism in which a process can be swapped temporarily out of main memory to secondary storage and make that memory available to other processes. In contrast, paging is a memory management technique in which a computer stores and retrieves data from secondary storage for use in main memory. These definitions explain the fundamental difference between swapping and paging.

Main Functionality

In swapping, the processes move back and forth between the main memory and secondary memory. In paging, equal size memory blocks, called pages, move between the main memory and secondary memory. Hence, the functionality is the main difference between swapping and paging.

Flexibility

Flexibility is also a difference between swapping and paging. Paging is more flexible than swapping because it transfers pages.

Number of Processes in the Main Memory

In swapping, there are more processes in the main memory. However, in paging, there are fewer processes in the main memory. Thus, this is another difference between swapping and paging.

Workloads

While swapping is suitable for heavy workloads, paging is suitable for small to medium workloads.

Usage

One other difference between swapping and paging is the usage. Swapping helps the CPU to access processes faster while paging helps to implement virtual memory.

Conclusion

Swapping and paging are two memory management techniques. The main difference between swapping and paging is that, in swapping, the processes move back and forth between the main memory and secondary memory while in paging, equal size memory blocks called pages moves between the main memory and secondary memory.

Reference:

1. “Swapping, Swap in, Swap out in Operating Systems (OS).” State Transition Diagram with Example in Software Engineering, Available here.
2. “Paging.” Wikipedia, Wikimedia Foundation, 24 Nov. 2018, Available here.

Image Courtesy:

1. “Memory paging” By Чоймаа – Own work (CC BY-SA 3.0) via Commons Wikimedia

About the Author: Lithmee

Lithmee holds a Bachelor of Science degree in Computer Systems Engineering and is reading for her Master’s degree in Computer Science. She is passionate about sharing her knowldge in the areas of programming, data science, and computer systems.

Leave a Reply