Swapping in operating system

Swapping in operating system

5 mins read31.2K Views Comment
Updated on Oct 13, 2023 13:29 IST

swapping is a memory management technique for swapping data between main memory and secondary memory for better memory utilization.This article explains swapping concept in detail with real-life example.

2022_07_ll.jpg

Swapping is a memory management technique that can be used to increase the operating system’s performance. This article describes Swapping in the operating system, and how to swap a memory management technique in an operating system.

  • Checking for application updates. 
  • Checking the incoming network, incoming mail, and so on. 

All these processes are brought into the main memory. Today’s single-user applications require a lot of disk space to boot. Therefore, keeping all these processes in the main memory requires a lot of main memory. Also, as the main memory increases, the cost of the system increases. To deal with these kinds of memory overloads, we have come up with two approaches: swapping and virtual memory.

Also explore: What is Operating Systems (OS) – Types, Functions, and Examples

Memory Management Techniques in Operating System
Memory Management Techniques in Operating System
Memory management is very important aspect of operating system performance.In this article we have covered different memory management techniques like paging,swapping,compaction and segmentation. This article covers different topics related to...read more
Interrupts in Operating System
Interrupts in Operating System
This article focuses on operating system interrupts. It also focuses on handling interrupts.
Working of Cache Memory in Computer
Working of Cache Memory in Computer
This article is focussed on cache memory and its different types.It also explains the working of cache memory in computer.Cache memory in computer is a small amount of random-access memory...read more

What is swapping? 

Swapping is moving data between physical memory(RAM) and secondary memory. In computing, virtual memory is a management technique that combines a computer’s hard disk space with its random access memory (RAM) to create a larger virtual address space. This can be useful if you have too many processes running on your system and not enough physical memory to store them.

While performing swapping the operating system needs to allocate a block of memory. It finds the first vacant block of physical memory.

As each new block of memory is allocated, it replaces the oldest block in physical memory. When a program attempts to access a page that has been swapped out, the operating system copies the page from the disk into physical memory and updates the page table entry.

The purpose of operating system swapping is to increase the degree of multiprogramming and increase main memory usage. 

 There are two steps to changing the operating system: 

  1. Swap-in: A swap-in process in which a process moves from secondary storage / hard disk to main memory (RAM). 
  2. Swap out: Swap out takes a process out of the main memory and places it in secondary memory.

Need of swapping in operating system

The main purpose of swapping in memory management is to allow more available memory than the computer hardware. Physical memory may be allocated, and the process may require additional memory. Rather than constraining the system to use only memory based on physical RAM, memory swapping allows the operating system and its users to extend the memory to disk.

Real-time Operating System
Real-time Operating System
Real time operating system is an important topic from interview point of view.This article includes real time operating system types and their examples.It also includes applications of real time system....read more
Difference Between Process and Thread
Difference Between Process and Thread
Confused about how your computer handles multiple tasks at once? Demystify the difference between processes and threads, the hidden engines driving parallelism on your machine. Learn how they share resources,...read more
Interprocess Communication in Operating System
Interprocess Communication in Operating System
Processes in operating systems need to communicate with each other. Interprocess communication (IPC) is a process that allows different processes of a computer system to share information. If you want...read more

Advantages of Swapping

  • Swapping can help to make more room and allow your programs to run more smoothly. Additionally, swap files can also be helpful in cases where you have multiple programs open at the same time. 
  • Using a swap file, you can ensure that each program has its own dedicated chunk of memory, which can help improve overall performance.
  • Improve the degree of multi-programming.
  • Better RAM utilization.

Disadvantages of Swapping

  • If the computer system is turned off during high paging activity, the user may lose all information related to the program. 
  • The number of page faults increases, which can reduce overall processing performance. 
  • When you make a lot of transactions, users lose information and computers lose power.

Working of Swapping technique in Operating System

Swapping means process exchange. Process exchange also relies on priority-based preemptive scheduling. That means when a higher priority process arrives, the memory manager temporarily swaps out the lowest priority process to disk, and the highest priority process is swapped in the main memory for execution. When the highest priority process terminates, the lower priority process is swapped back to memory and continues to run. And this process is called swapping.

Due to the address binding method, processes paged out of main memory occupy the same address space when paged back to main memory if binding occurs during load time. If it would have run-time binding, the address is calculated at run time so that the process can use any address space available in the main memory.

When the process is swapped in and out, it creates multiple holes in the memory. The question must be coming to your mind. What to do with these holes? In that case, holes are combined to create a big memory space. This is done by moving all the processes downwards as far as possible. This technique is called compaction. But, doing this requires a lot of CPU time. So typically, this technique is not used. Like in the below diagram, the free space available is 40+20+20 K.This available space is not available in a contiguous way. So this free space is compacted to make 80 K space.

Swap file and Swap space

Swap space is used as a temporary storage capacity when the physical space is already exhausted. A paging file is a physical disk storage file for swap space that the operating system uses to expand the available memory. The operating system reuses physical memory over time, so paging files and associated memory pages can be recreated in different areas of the system’s virtual memory. 

What is swap space for? 

Swap space helps make your computer’s operating system look like it has more RAM than it is. Also known as a paging file. This data exchange between virtual and real memory is called swap space, and disk space is called “swap space.”

Difference between swapping and paging

Criteria Paging Swapping
When to perform Paging is done when only the required pages are swapped out. Swapping is done when the entire process is swapped out to disk
Cost Paging is less expensive. Swapping is more expensive than paging, as it requires more disk access. This is because the entire process needs to be read from disk and written back to disk, instead of just the required pages.
Efficiency Paging, on the other hand, is much more efficient. It only requires a small number of disk accesses It is less efficient than paging.
Workload The paging technique is used for less workload. For high workloads, the Swapping method is used.

Conclusion

If swapping is done correctly, it can be incredibly effective in keeping your computer running smoothly.

However, if not done correctly, it can lead to decreased performance, and even system crashes. In this article, we have explored Swapping in operating systems and taught you the working of the swapping technique. If you liked this article hit the like button below and share it with your friends.

Keep learning keep sharing!!!

Happy learning

______________

Recently completed any professional course/certification from the market? Tell us what you liked or disliked in the course for more curated content.

Click here to submit its review with Shiksha Online

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio