Linux swap space Explained with Examples | What is the swap space | How does the swap space work

Описание к видео Linux swap space Explained with Examples | What is the swap space | How does the swap space work

This video explains the following topics.
What is the swap space?
How does swap work?
How Linux uses the swap space
Linux swap space
swap space in Linux
When Linux uses the swap space
When Linux does not use the swap space
What is the threshold value on the swap space

This video is based on the following tutorial.
https://www.computernetworkingnotes.c...

Linux swap space
Developers developed Linux primarily for server systems. A server system runs multiple services simultaneously. Sometimes, a process may use more memory than expected. For example, a university website hosted on a Linux server may consume more memory on exam results day. This type of memory spike is natural on server systems. Any service may use more memory at its peak time. Linux uses swap space to deal with the lack of physical memory. A swap space is a hard disk space. Linux uses it to extend the physical memory.
How does swap work?
The system divides RAM into smaller chunks known as memory pages. It assigns memory pages to the service or process running in the CPU. CPU executes services or applications in a round-robin fashion.
A round-robin fashion schedules all processes in a queue. It assigns a quantum (allowed CPU time slot) to each process in the queue. If the process does not end in its time slot, the CPU pauses it and takes the upcoming process from the queue. If the subsequent process does not finish in its time slot, the CPU pauses it and picks the upcoming process from the queue.
Let us take an example. A CPU has two processes to execute. A quantum is 100 milliseconds. The first process needs 350 milliseconds, and the second process needs 140 milliseconds to complete.
The CPU executes the first process. After 100 milliseconds, it suspends the first process and starts the second process. It runs the second process for 100 milliseconds. After 100 milliseconds, it pauses the second process and resumes the first process. It runs the first process for another 100 milliseconds. After 100 milliseconds, it again stops the first process and starts the second process. The second process needs only 40 milliseconds to complete. It allocates the remaining 60 milliseconds of the time slot to the first process. In the time slot, the first process also ends. Since no other process is running, the CPU becomes ideal. The CPU repeats this cycle each time it gets new processes to run.
Every process needs memory pages on RAM to store its data. An application can run many processes simultaneously. The more applications you run, the less memory remains available for other applications. If no memory is left, you can not start a new process.
The swap space solves this problem. As explained earlier, the CPU runs only one process at a given time and keeps all other processes paused. If the swap space is available, the system moves ideal processes' data to the swap space.

Threshold

A swap space resides on the hard disk. A hard disk is a secondary memory. It cannot replace the primary memory. Secondary memory is slower than primary memory.
The CPU uses the primary memory to process the data. It does not access data directly from the hard disk. It means the CPU will process an application's data only when the data is available on RAM. If the operating system uses the swap space, it has to move out the ideal application's data and move in the active application's data every quantum. It creates a lot of overhead. It decreases the system's performance instead of improving it. Because of this, the system uses the swap space only when it has a shortage of RAM. It defines a threshold value for RAM.
It uses the swap space only when the uses of the RAM crosses the threshold.

Key points:-
The swap space is a hard disk space. The system uses it to store idle memory pages.
Since the swap space resides on the hard disk, it is slower than with RAM.
The swap space is an essential part of Linux configuration.
The system automatically configures a threshold value if the swap space is available.
The system will use the swap space only when the use of RAM reaches the threshold.
As long as the use of the RAM remains below the threshold, the system does not use the swap space.

Комментарии

Информация по комментариям в разработке