Swap Space for Linux:

How Much is Re­ally Needed?

OpenSource For You - - FOR U & ME OVERVIEW - By: Roopak T J The au­thor is an open source con­trib­u­tor and en­thu­si­ast. He has con­trib­uted to a cou­ple of open source or­gan­i­sa­tions in­clud­ing Me­di­awiki and Li­breOf­fice. He is cur­rently in his sec­ond year at Am­rita Univer­sity (B. Tech). You can con­tact him

Linux di­vides its phys­i­cal mem­ory (RAM) into chunks called pages. Swap­ping is the process whereby pages get trans­ferred to a pre­con­fig­ured hard disk area. The quan­tum of swap space is de­ter­mined dur­ing the Linux in­stal­la­tion process. This ar­ti­cle is all about swap space, and ex­plains the term in de­tail so that new­bies don’t find it a prob­lem choos­ing the right amount of it when in­stalling Linux.

The vir­tual mem­ory of any sys­tem is a com­bi­na­tion of two things - phys­i­cal mem­ory, which can be ac­cessed, i. e., RAM, and swap space. The lat­ter holds the in­ac­tive pages that are not ac­cessed by any run­ning ap­pli­ca­tion. Swap space is used when the RAM has in­suf­fi­cient space for ac­tive pro­cesses, but it has cer­tain spa­ces which are in­ac­tive at that point in time. These in­ac­tive pages are tem­po­rar­ily trans­ferred to the swap space, which frees up space in the RAM for ac­tive pro­cesses. Hence, the swap space acts as tem­po­rary stor­age that is re­quired if there is in­suf­fi­cient space in your RAM for ac­tive pro­cesses. But as soon as the ap­pli­ca­tion is closed, the files that were tem­po­rar­ily stored in the swap space are trans­ferred back to the RAM. The ac­cess time for swap space is less. In short, swap­ping is re­quired for two rea­sons:

When more mem­ory than is avail­able in phys­i­cal mem­ory (RAM) is re­quired by the sys­tem, the ker­nel swaps lessused pages and gives the sys­tem enough mem­ory to run the ap­pli­ca­tion smoothly.

Cer­tain pages are re­quired by the ap­pli­ca­tion only at the time of ini­tial­i­sa­tion and never again. Such files are trans­ferred to the swap space as soon as the ap­pli­ca­tion ac­cesses these pages.

Af­ter un­der­stand­ing the ba­sic con­cept of swap space, one should know what amount of space needs to be ac­tu­ally al­lot­ted to the swap space so that the per­for­mance of Linux ac­tu­ally im­proves. An ear­lier rule stated that the amount of swap space should be dou­ble the amount of phys­i­cal mem­ory (RAM) avail­able, i.e., if we have 16 GB of RAM, then we ought to al­lot 32 GB to the swap space. But this is not very ef­fec­tive these days.

Ac­tu­ally, the amount of swap space de­pends on the kind of ap­pli­ca­tion you run and the kind of user you are. If you are a hacker, you need to fol­low the old rule. If you fre­quently use hi­ber­na­tion, then you would need more swap space be­cause dur­ing hi­ber­na­tion, the ker­nel trans­fers all the files from the mem­ory to the swap area.

So how can the swap space im­prove the per­for­mance of Linux? Some­times, RAM is used as a disk cache rather than to store pro­gram mem­ory. It is, there­fore, bet­ter to swap out a pro­gram that is in­ac­tive at that mo­ment and, in­stead, keep the of­ten-used files in cache. Re­spon­sive­ness is im­proved by swap­ping pages out when the sys­tem is idle, rather than when the mem­ory is full.

Even though we know that swap­ping has many ad­van­tages, it does not nec­es­sar­ily im­prove the per­for­mance of Linux on your sys­tem, al­ways. Swap­ping can even make your sys­tem slow if the right quan­tity of it is not al­lot­ted. There are cer­tain ba­sic con­cepts be­hind this also. Com­pared to mem­ory, disks are very slow. Mem­ory can be ac­cessed in nanosec­onds, while disks are ac­cessed by the pro­ces­sor in mil­lisec­onds. Ac­cess­ing the disk can be many times slower than ac­cess­ing the phys­i­cal mem­ory. Hence, the more the swap­ping, the slower the sys­tem. We should know the amount of space that we need to al­lot for swap­ping. The

fol­low­ing rules can ef­fec­tively help to im­prove Linux’s per­for­mance on your sys­tem. For nor­mal 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 stor­age re­quire­ments: 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 al­ready in­stalled Linux, you can check your swap space by us­ing the fol­low­ing com­mand in the Linux ter­mi­nal:

cat /proc/swaps

Swap­pi­ness and how to change it

Swap­pi­ness is a pa­ram­e­ter that con­trols the ten­dency of the ker­nel to trans­fer the pro­cesses from phys­i­cal mem­ory to ‘swap space’. It has a value be­tween 0 to 100 and in Ubuntu, it has a de­fault value of 60. To check the swap­pi­ness value, use the fol­low­ing com­mand:

cat /proc/sys/vm/swap­pi­ness

A tem­po­rary change (lost at re­boot) in a swap­pi­ness value of 10, for ex­am­ple, can be done with the fol­low­ing com­mand:

su­dosysctlvm.swap­pi­ness=10

For a per­ma­nent change, edit the con­fig­u­ra­tion file as fol­lows:

gk­su­dogedit /etc/sysctl.conf

If the swap­pi­ness value is 0, then the ker­nel re­stricts the swap­ping process; and if the value is 100, the ker­nel swaps very ag­gres­sively.

So, while Linux as an op­er­at­ing sys­tem has great pow­ers, you should know how to use those pow­ers ef­fec­tively so that you can im­prove the per­for­mance of your sys­tem.

Newspapers in English

Newspapers from India

© PressReader. All rights reserved.