Swap Space for Linux:
How Much is Really Needed?
Linux divides its physical memory (RAM) into chunks called pages. Swapping is the process whereby pages get transferred to a preconfigured hard disk area. The quantum of swap space is determined during the Linux installation process. This article is all about swap space, and explains the term in detail so that newbies don’t find it a problem choosing the right amount of it when installing Linux.
The virtual memory of any system is a combination of two things - physical memory, which can be accessed, i. e., RAM, and swap space. The latter holds the inactive pages that are not accessed by any running application. Swap space is used when the RAM has insufficient space for active processes, but it has certain spaces which are inactive at that point in time. These inactive pages are temporarily transferred to the swap space, which frees up space in the RAM for active processes. Hence, the swap space acts as temporary storage that is required if there is insufficient space in your RAM for active processes. But as soon as the application is closed, the files that were temporarily stored in the swap space are transferred back to the RAM. The access time for swap space is less. In short, swapping is required for two reasons:
When more memory than is available in physical memory (RAM) is required by the system, the kernel swaps lessused pages and gives the system enough memory to run the application smoothly.
Certain pages are required by the application only at the time of initialisation and never again. Such files are transferred to the swap space as soon as the application accesses these pages.
After understanding the basic concept of swap space, one should know what amount of space needs to be actually allotted to the swap space so that the performance of Linux actually improves. An earlier rule stated that the amount of swap space should be double the amount of physical memory (RAM) available, i.e., if we have 16 GB of RAM, then we ought to allot 32 GB to the swap space. But this is not very effective these days.
Actually, the amount of swap space depends on the kind of application you run and the kind of user you are. If you are a hacker, you need to follow the old rule. If you frequently use hibernation, then you would need more swap space because during hibernation, the kernel transfers all the files from the memory to the swap area.
So how can the swap space improve the performance of Linux? Sometimes, RAM is used as a disk cache rather than to store program memory. It is, therefore, better to swap out a program that is inactive at that moment and, instead, keep the often-used files in cache. Responsiveness is improved by swapping pages out when the system is idle, rather than when the memory is full.
Even though we know that swapping has many advantages, it does not necessarily improve the performance of Linux on your system, always. Swapping can even make your system slow if the right quantity of it is not allotted. There are certain basic concepts behind this also. Compared to memory, disks are very slow. Memory can be accessed in nanoseconds, while disks are accessed by the processor in milliseconds. Accessing the disk can be many times slower than accessing the physical memory. Hence, the more the swapping, the slower the system. We should know the amount of space that we need to allot for swapping. The
following rules can effectively help to improve Linux’s performance on your system. For normal servers: Swap space should be equal to RAM size if RAM size is less than 2 GB. Swap space should be equal to 2 GB if RAM size is greater than 2 GB. For heavy duty servers with fast storage requirements: Swap space should be equal to RAM size if RAM size is less than 8 GB. Swap space should be equal to 0.5 times the size of the RAM if the RAM size is greater than 8 GB. If you have already installed Linux, you can check your swap space by using the following command in the Linux terminal:
Swappiness and how to change it
Swappiness is a parameter that controls the tendency of the kernel to transfer the processes from physical memory to ‘swap space’. It has a value between 0 to 100 and in Ubuntu, it has a default value of 60. To check the swappiness value, use the following command:
A temporary change (lost at reboot) in a swappiness value of 10, for example, can be done with the following command:
For a permanent change, edit the configuration file as follows:
If the swappiness value is 0, then the kernel restricts the swapping process; and if the value is 100, the kernel swaps very aggressively.
So, while Linux as an operating system has great powers, you should know how to use those powers effectively so that you can improve the performance of your system.