How to Add Swap Partition on Linux
When you install Linux, the setup process will likely add a swap partition to the hard disk along with the partitions for Linux itself. But what is this swap partition used for and is it really necessary?
What Is Swap?
When a multitasking operating system runs out of physical memory, it is unable to start new processes or allocate more memory to existing processes. To solve this problem, a special block of space on the local storage (often the hard drive) is set aside to act as a temporary holding area for processes that aren’t currently running. The OS “swaps” the inactive process out of memory and on to the hard disk. This frees up memory for new work. When the inactive process becomes active again, it will be swapped back from the local storage to the memory. This procedure is known as swapping.
Is Swap Space Really Necessary?
Is swap space technically necessary? No. Linux can work without it and in some situations, like embedded solutions, Linux can be run without swap. If the OS gets into a situation where it doesn’t have enough memory, it will panic and stop. If there is enough physical RAM, the machine will run without any problems.
Is swap space practically necessary? Yes. Although swapping slows down a computer (as reading and writing to a hard disk is much slower than accessing RAM) and having as much physical memory on board is the best solution to avoid swapping, there are legitimate situations where swapping is desirable. For example, during system boot, there can be processes that run (and so consume system memory) but then effectively sleep for very long periods of time. Or memory that is allocated, written to and then never used again.
In these situations, the swapping algorithm will start to migrate this memory to the hard disk and so free up valuable system memory. This actually ensures that the maximum amount of RAM is available at all times and so increases performance.
On machines that don’t have enough physical RAM for everything to reside in memory then the use of swap space is essential!
For completeness, it is worth mentioning that Linux also has a subsystem called the Out-Of-Memory Killer (OOM), which rather than stopping the kernel when it runs out of memory, will start to kill of processes to free resources. Its actions are configurable.
How Much Swap Space Should I Allocate?
There is no absolute rule and since hard disk space is cheap (dollar per Gigabyte) it is better to allocate too much than too little space. As a rule of thumb, you should have two or three times the amount of swap space as physical memory.
Adding Swap Without Expanding Swap Partition
There are two ways to add swap space to a system without altering the existing swap partition. One way is to use a swap partition on another drive, the other is to use a swap file.
To use another hard drive, create a partition of type “Linux swap” (code 82) and then run the “
mkswap” command to prepare the swap space:
/dev/sdb2 is the name of the partition you created. To use the swap space immediately use the “
swapon -s” to get a list of the currently used swap spaces. To permanently add the swap space to the system, you will need to edit the
/etc/fstab file and add a line similar to this:
If your system is using unique identifiers to name the disks, use the
blkid command to get a list of ids for the devices. The line to mount the swap space in
/etc/fstab would then look something like this:
To use a file for swap, create a large blank file (using
dd) on the filesystem that you want to use for swapping. For example, to create a 1GB file, type:
/store/swapfile is the path to the swap file on the desired filesystem.
As with the additional swap partition, the file now needs to be prepared with
mkswap and then used with
swapon. It also needs to be added to the
/etc/fstab file. The procedure is exactly the same as above, but you need to use
/store/swapfile rather than
/dev/sdb2 as the swap space parameter.
The Linux kernel can be tweaked to define how aggressively it should try to swap processes out of memory. This tendency is controlled by a kernel variable called
swappiness of 0 means that the kernel will avoid swapping as much as possible, while 100 means the kernel will be aggressive in how it uses the swap space. The default setting for many Linux distributions is 60. It is possible to change the number dynamically using this command:
To permanently set a value, you need to change (or add, if it doesn’t exist) the
vm.swappiness variable in the
Let us know if you have questions about swap in Linux.