The swap space on Linux Explained

A swap space is a temporary hard disk space. Linux uses it as RAM. Linux uses it only when it has a shortage of RAM. This tutorial explains the need for the swap space and how Linux uses it.

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 space 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 next process from the queue. If the subsequent process does not finish in its time slot, the CPU pauses it and picks the next 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 next 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.

Let us take an example.

The system has 4GB RAM. When we start the system, the system loads Linux into RAM. Linux runs from RAM. Each operating system reserves a specific portion of RAM for it. It runs the application only in the remaining RAM. Let us suppose Linux is using 3GB RAM. In this case, only 1GB RAM is available for applications. You start Adobe Photoshop. It needs 600Mb RAM. Then, you start Firefox. It uses 400Mb RAM. Now, no RAM is remaining for another process. You cannot open a new process.

ram full

But if the swap space is available, it will move the FireFox's data to the swap space when it is processing Adobe Photoshop's data. Similarly, when it processes FireFox's data, it moves Adobe Photoshop's data to the swap space. It keeps only the active application's data in RAM. Since it does not store the ideal application's data in RAM, the RAM space the ideal application's data needs remain free. You can use this free RAM space to start a new process. This way, the swap space allows Linux to run more applications than RAM can hold.

swap out

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. Moving the ideal application's data out and the active application's data in every quantum, 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.

Let us take an example.

Suppose, in the preceding example, the threshold value is 3.5GB. You start Firefox and FileZilla applications. Let us suppose FileZilla needs 50Mb RAM. In this case, the total use of RAM will be 3.45GB.

3.00 GB (Operating system) + 400Mb (Firefox) + 50Mb (FileZilla) = 3.45GB.

below threshold

Since the total use of the RAM is less than the threshold value, it will not use the swap space. It will keep both application's data in RAM. But if you start one more application that needs 100Mb of RAM, the total uses of the RAM will cross the threshold value. In this case, it will use the swap space until you close at least one application.

above threshold

As soon as you terminate an application, the total uses of the RAM will reduce below the threshold value, and it will stop using the swap space again. This way, it will move ideal applications' data to the swap space only when the total uses of the RAM will cross the threshold value.

below 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.

ComputerNetworkingNotes Linux Tutorials The swap space on Linux Explained