Swapping in Operating System
Swapping is a memory management technique for moving data between the main memory and secondary memory to improve memory utilization.This article explains the swapping concept in detail with a real-life example.
Swapping is a memory management technique that can 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.
Explore Interprocess Communication in Operating System
Table of contents
Best-suited Operating Systems courses for you
Learn Operating Systems with these high-rated online courses
Real-life example
When the Windows operating system starts, many processes start running as the system starts the booting process. These processes do-
- 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, so keeping all these processes in the main memory requires a lot of memory. Also, as the main memory increases, the system's cost increases. To deal with these kinds of memory overloads, we have devised two approaches: swapping and virtual memory.
Also explore: What is Operating Systems (OS) – Types, Functions, and Examples
Also explore: Operating system interview questions
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 must allocate a memory block. It finds the first vacant block of physical memory.
As each new memory block 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:
- Swap-in: A swap-in process in which a process moves from secondary storage / hard disk to main memory (RAM).
- Swap out: Swap out takes a process from 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.
Advantages of Swapping
- Swapping files can free up space and allow your programs to run more smoothly. It 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 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. 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 had run-time binding, the address would have been calculated at run-time so that the process could 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. However doing this requires a lot of CPU time. So, typically, this technique is not used. As 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 to reduce the 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, swapping can lead to decreased performance and even system crashes. In this article, we have explored Swapping in operating systems and taught you how the swapping technique works. If you liked this article, hit the like button below and share it with your friends.
Keep learning, and keep sharing!!!
Happy learning
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