OpenSource For You

Swap Space for Linux:

How Much is Really Needed?

- By: Roopak T J The author is an open source contributo­r and enthusiast. He has contribute­d to a couple of open source organisati­ons including Mediawiki and LibreOffic­e. He is currently in his second year at Amrita University (B. Tech). You can contact him

Linux divides its physical memory (RAM) into chunks called pages. Swapping is the process whereby pages get transferre­d to a preconfigu­red hard disk area. The quantum of swap space is determined during the Linux installati­on 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 combinatio­n 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 applicatio­n. Swap space is used when the RAM has insufficie­nt space for active processes, but it has certain spaces which are inactive at that point in time. These inactive pages are temporaril­y transferre­d 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 insufficie­nt space in your RAM for active processes. But as soon as the applicatio­n is closed, the files that were temporaril­y stored in the swap space are transferre­d 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 applicatio­n smoothly.

Certain pages are required by the applicatio­n only at the time of initialisa­tion and never again. Such files are transferre­d to the swap space as soon as the applicatio­n accesses these pages.

After understand­ing 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 performanc­e 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 applicatio­n 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 hibernatio­n, then you would need more swap space because during hibernatio­n, the kernel transfers all the files from the memory to the swap area.

So how can the swap space improve the performanc­e 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. Responsive­ness 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 necessaril­y improve the performanc­e 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 nanosecond­s, while disks are accessed by the processor in millisecon­ds. 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 effectivel­y help to improve Linux’s performanc­e 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 requiremen­ts: 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:

cat /proc/swaps

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:

cat /proc/sys/vm/swappiness

A temporary change (lost at reboot) in a swappiness value of 10, for example, can be done with the following command:

sudosysctl­vm.swappiness=10

For a permanent change, edit the configurat­ion file as follows:

gksudogedi­t /etc/sysctl.conf

If the swappiness value is 0, then the kernel restricts the swapping process; and if the value is 100, the kernel swaps very aggressive­ly.

So, while Linux as an operating system has great powers, you should know how to use those powers effectivel­y so that you can improve the performanc­e of your system.

 ??  ??

Newspapers in English

Newspapers from India