‘Swapping Space’ For Linux –But how much is actually needed?

All of us might have seen an option asking for Swap space at the time of Linux installation and we might have also allotted certain memory 2GB, 4GB or so on for that. But have you ever wondered what actually this Swap space is and how it can affect the performance of your Linux operating system. We also might have thought how much space should we provide if we have this much amount of RAM (Random Access Memory) available with us. Or is this really needed if we already have large amount of RAM available with us. So, this article is all about Swap space, and also explains the term from very basic so that newbies or beginners don’t find it a problem to choose the Swap space at the time of Linux Installation.

Virtual Memory of any system is a combination of two things- Physical memory, which can be accessed, i.e., RAM and Swap space. Swap space holds the inactive pages which are not accessed by any running application. Swap space is used when your RAM has insufficient space for active processes but it has certain spaces which are inactive at present. In that case those inactive pages are temporarily transferred to the Swap space freeing space in RAM for active processes. Hence Swap space acts as a temporary storage which is required if there is insufficient space in your RAM for active processes. But as soon as the application is closed, those files temporarily stored in Swap space are transferred back to the RAM. Access time for swap space is less. In short swapping is importantly required for two reasons:-Firstly when excess memory, than available in physical memory (RAM) is required by the system, the kernel swaps less used page and gives the system enough memory to run the application smoothly. Secondly certain pages are required by the application only at the time of initialization 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 we should actually allot to the Swap Space so that the performance of our Linux actually improves. It has been an earlier concept which states that the amount of Swap Space should be double the amount of physical memory (RAM) available with us ,i.e., if we have 16GB RAM, then we should allot 32GB to the Swap Space. But this concept is not much effective these days. Actually the amount of Swap Space depends on the kind of application you run and the kind of user you are. Suppose if you are a hacker, then you need to follow the “old rule”. Or if frequently use hibernation, you would need more Swap Space area because during hibernation, the kernel transfers all the files from memory to the swap area. So, the Swap space should be equal to or greater than the RAM available with us.

Now the question arises is how can Swap Space improve the performance of your Linux. Sometimes RAM is used for disk cache than to store program memory. So, it is better to swap out a program that is inactive at present, and instead keep the often used files in cache. Secondly, responsiveness is improved by swapping pages out when the system is idle, rather than when the memory is full.

Since we know that Swapping has many advantages, it is not always necessary that it will improve your Linux’s performance. Swapping can even make your system slow if not done in right quantity. 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 in milliseconds by the processor. Accessing the disk can be many times slower than accessing the physical memory. Hence more is the Swapping, slower will be the system. So, we should know the amount of space which we should allot for Swapping. The following rule can effectively help to improve your Linux’s performance:

– For normal servers:

  • Swap space should be equal to RAM size if RAM size is less than 2GB.

  • Swap space should be equal to 2GB size if RAM size is greater than 2GB.

-For heavy duty servers with fast storage purpose:

  • Swap space should be equal to RAM size if RAM size is less than 8GB.

  • Swap space should be equal to 0.5 times the size of the RAM if the RAM size is greater than 8GB

If you have already installed Linux, u can even check your Swap space by the following command in linux terminal

free -m

Swappiness and how to change it?

Swappiness is a parameter which controls the tendency of kernel to transfer the processes from physical memory to ‘Swap space’. It has value between 0 to 100 and in Ubuntu it has a default value of 60. To check the swappiness value, we can use the command

cat /proc/sys/vm/swappiness

To change the Swappiness value

  • For temporary change(lost at reboot) with a value of 10 can be done with

sudo sysctl vm.swappiness=10

  • For permanent change,edit the configuration file

gksudo gedit /etc/sysctl.conf

If the Swappiness value is 0, then the kernel restricts the swapping process and if the value is 100, then the Kernel swaps very aggressively.

So, the Linux as an Operating System has great powers but you should know how to utilise those powers effectively so that you can improve the performance of your system.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s