What You Need to Know About Swap Partitions 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?

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 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.

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.

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:

Where /dev/sdb2 is the name of the partition you created. To use the swap space immediately use the “swapon” command:

Use “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:

Where /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. A 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 /etc/sysctl.conf file.

Let us know if you have questions about swap in Linux.


  1. Very informative article.
    It will be interesting to know few examples which process takes more ram on linux.
    For example if i open 5 youtube video tabs in firefox. How much memory does it take?

    How can i hang computer (doing what) without swap partition if i have like 8 gigs of ram. Video editing, etc.

    I have experience that httrack for big projects really needs a lot of RAM :)

  2. Does it matter where swap partition is located on the hard drive (beginning, middle or the end)?

    Does putting the swap partition on a separate drive and controller speed up the swap process somewhat?

    • No, it doesn’t matter where on the hard drive the swap partition is located. And yes, putting the swap partition on a separate drive will speed it up. This is because the drive heads will stay at the spot they last wrote to, which is also the spot they’ll *next* write to, and so won’t have to seek back to that spot before writing.

    • Yes, it does matter. Running hard disks keep read/write heads over track one, which provides faster access to directory information at the beginning of the platter. (When the drive powers down it safely “parks” the head to an outer track.) If the swap partition is at the middle or end of the disk then it will take more seek time to get at the swap data. (The actual continuous disk to head data rate is faster at the outer edge but disk access time negates that advantage.) If the swap is at the beginning then non-cached directory information (to access files) will be impacted, no advantage gained. Getting a second drive is mental because a swap space for say 6GB RAM would be less than 20 GB which would be a slower last generation drive. Linux (since at least Kernel 2.6) can use a swapfile like Windows has had. Re speed of access: most large server installations (e.g. Google) are clusters of virtual servers which reside on virtual drives which are simply large files. (Many precompiled/binary Linux distributions required a separate swap partition, however if you “make” or “roll your own” distribution then swap-to-file, in the Kernel, was an option. Of course if you use a BSD based OS like Mac OS X instead of a memory pig like Windows or (copycat) Linux then you wouldn’t have the memory problems in the first place.

      • Steve,

        Modern filesystems don’t store the directory information at the beginning of platter.

        I must say in all my years of using and writing about computers I never heard anybody call Linux a copycat of Windows. That is certainly a unique idea!

        Also I find that my Mac consumes more memory than my Linux box.

        Ta, Gary.

      • This is quite literally one of the most misinformed replies I have ever read. It makes Jenny McCarthy sound like a genious.

  3. On machines that have to use swap often then putting the swap space on a disk with less I/O will make swap faster. Also if Linux has multiple swap spaces available it will alternate between them which improves performance. A true high performance swap setup would be two separate disks dedicated entirely to swap. Of course, it would be better to add more memory instead!!!!

    • Or RAID 0 swap partition? But then how many machines have more than one hard disk? Of course for the price of a second hard drive one could buy a lot more RAM. I set up Servers with 3+ hard drives (Raid 5; Raid 1 plus hot spare; etc.) but the godawful noise of multiple high speed hard disks makes for an unpleasant desktop.

      • My desktop has four hard disks and it makes the same amount as noise as it does with one disk!

  4. Yes, as Aigars said, a very informative article. I’ve been “toying” with various distros of Linux for years, but had never come across the fact that you can swap to a file. That info could come in useful in the future.

    • Not a good idea, Jason. Because of the technology involved an SSD has only a limited number of writes. Even with cheap drives, using SSD as swap could get expensive fast.

      • That is not so, as they can take more writes than most expect. And if you use all of the disk it will level out the writes on the disk.

        • And how many writes will there be to swap during a normal session? Or during heavy use? There are too many variables to predict how quickly the writes are exhausted. Maybe you don’t mind replacing SSDs every couple of weeks but I do..

          • Then you have not read up on how SSD discs actually works and how long they will survive on heavy read and writes.
            It will last years.

  5. Usually when I see figures about swap size, the usuall suggestion are same or double size. More will ususall only slow down the computer. But as usually, your figures may be different.
    Why the same? Because then you can hibernate on the swap disk. Why not more than twice? Because if you swap more you are likely to use the swap disk so much the machine might “stop working” and just swap. You would “trash” the system.

Comments are closed.

Sponsored Stories