Linux Technology Explained

What Is a Linux Swap Partition? Everything You Need to Know

Bertel King Updated 20-05-2020

Most Linux installations recommend that you include a swap partition. This may seem odd to Windows users used to having their entire operating system on a single partition.

Advertisement

What does a swap partition do, do you even need one, and how big should it be? These are all important questions that, with the right answers, can seriously improve your system’s performance.

What the Linux Swap Partition Does

Partitions in Ubuntu Linux

The swap partition serves as overflow space for your RAM. If your RAM fills up completely, any additional applications will run off the swap partition rather than RAM.

This may sound like an easy way to increase your computer’s amount of usable memory without actually getting more RAM, but that isn’t the case. RAM is the ideal hardware for memory because it’s extremely quick, unlike hard drives which are, relatively speaking, much slower.

Solid state drives may have made the performance hit less of an issue with their much-improved speeds How Do Solid-State Drives Work? In this article, you'll learn exactly what SSDs are, how SSDs actually work and operate, why SSDs are so useful, and the one major downside to SSDs. Read More , but even they can’t match RAM. This is also true of newer NVMe SSDs What Is an M.2 SSD? The Pros, Cons, and How to Install One Want your operating system to run even faster? The answer is to use an M.2 SSD drive. Here's what you need to know. Read More . In either case, you wouldn’t want to cause additional wear and tear on your solid state drive.

Advertisement

A close analogy of the swap partition is the Windows pagefile How to Troubleshoot Low RAM or Memory Leaks in Windows Do you have 4 or more GB of RAM and does your computer still feel sluggish? You may suffer from a memory leak or shortage. We show you how to troubleshoot all your Windows (10)... Read More , although there are many technical differences between the two.

The Linux swap partition is not limited to being overflow storage space. It can assist your PC in other ways.

Prioritization

A swap partition can also help move some items from your memory to your hard drive in order to leave more room in memory for more important items. This implies that items that are rarely touched would get moved to the swap partition.

The threshold of what is considered “rare” depends on the “swappiness” (yes, that’s the actual term used), which is configurable. A higher swappiness means data is more likely to be moved to the swap partition. A lower swappiness means data is less likely to be moved to the swap partition.

Advertisement

Hibernation

A swap partition is used as the destination of your memory’s contents whenever you tell your system to hibernate. This means that without a swap partition, hibernation on Linux is impossible.

That said, it has become rather rare for people to use the hibernation feature, so this may not matter to you.

Do You Need a Swap Partition?

Create swap partition in Ubuntu Linux

Does this mean that a swap partition is necessary? Not at all! A Linux system can perform perfectly well without a swap partition. We’ve already discussed the benefits of a swap partition. Now, why might you not want to have one?

Advertisement

When Swap Partitions Don’t Help

Swap partitions have their downsides. They take up space on your hard drive that doesn’t resize dynamically when not in use. Heavy swap usage can also increase wear and tear on your main drive. In some cases, swap partitions don’t even help improve performance. Here’s an example where having a swap partition can actually be worse than not having one.

Say you’ve installed Linux on an old netbook with only 2GB of RAM and a 5400rpm hard drive. With just 2GB of memory, you can imagine that filling up pretty quickly with a few open browser tabs. The swap partition allows you to keep them all open as the memory overflows How Much RAM Do You Really Need? How much computer memory do you need? Here's how to check your installed RAM and how much RAM your computer needs. Read More .

But then a bottleneck appears because of the hard drive’s 5400rpm speed. Since the hard drive is so slow and the system constantly wants to access the swap partition, the netbook becomes extremely sluggish. The machine is slow enough to be unusable unless you close everything to free up some memory.

The set swappiness doesn’t guarantee that everything in the swap partition will move back over once space becomes available in RAM. Instead, much may stay in the swap partition, causing the netbook to continue to be sluggish. So you’re left rebooting your computer to start from a clean slate, which takes a while because the system has to remove everything from the swap partition before shutting down.

Advertisement

What Happens When You Don’t Have Swap

If you do decide to forego a swap partition, know the risks. When your computer needs more RAM than is available, the interface can lock up. You risk having to force quit your computer and lose all the data you were working on.

In such cases, you may wish you had a swap partition around, even if it were only used that once. This depends on whether you find yourself running out of storage space often. Would you notice if you had 4GB less storage space available because you devoted that amount to swap?

Linux Swap Recommendations

Here are some recommendations for when you may want to have a swap partition and how large to make it.

  • If you would like to be able to hibernate your computer, then you should have a swap partition. The size of this partition should be the size of your installed memory, plus an additional 10-25% to leave room for any items that were already moved over into the swap partition.
  • Just want a small performance boost (and you have at least a 7200rpm hard drive)? Then you can add a swap partition if you want. The size of this can be whatever you’d like, but I wouldn’t make it any bigger than you would if you were creating a swap partition to enable hibernation.
  • If you occasionally use heavy applications that require extra RAM, a swap partition can serve as peace of mind. In this case, you don’t need your swap partition to be as large as your RAM.
  • If you have a 5400rpm hard drive, then you may not want to create a swap partition simply because the bottleneck can make your computer worse off. But if you absolutely want to have swap, then you can still create a partition using the same sizing guidelines outlined above. Just be sure to change the swappiness value to something much lower.

Changing Swappiness

Ubuntu Linux swappiness file

Like many aspects of the Linux desktop, your computer’s swappiness is stored in a text file. You can find this file by navigating to /proc/sys/vm.

When you open the file, you will see a single number indicating your current swappiness. You can edit this file using any text editor of your choice, as long as you have root permissions.

To do this with the default GNOME text editor found in Ubuntu and Fedora, you could try:

sudo gedit /proc/sys/vm/swappiness

There is also a command line option that works regardless of which text editor you have installed. Simply enter:

sudo sysctl vm.swappiness=20

You can enter any digit ranging from 0 to 100. The value indicates when you want Linux to start actively moving processes from the memory to the swap partition. So for example, a value of 20 indicates that processes will be moved when memory usage reaches 80%; the default swappiness value in Ubuntu of 60 indicates that processes will be moved when memory usage reaches 40%.

You can check whether the change was successful by reopening that text file. Unsurprisingly, the terminal offers a faster way to check your swappiness. Just enter this command:

cat /proc/sys/vm/swappiness

Does Your PC Feel Faster?

Swap partitions can make a major difference in your system’s performance—sometimes for better and sometimes for worse. Now that you know what the swap partition is for, hopefully you’re better equipped to make the decision appropriate for your situation.

But before you re-partition your drive, know that there’s more to memory management than the amount of RAM you have and the size of your Linux swap partition. Take a moment to learn how Linux manages RAM Is Linux Eating Your RAM? How to Manage Your Memory Why is Linux suddenly running slow? Here's how to check RAM usage and speed things up again on your Linux PC or laptop. Read More .

Related topics: Disk Partition, Hard Drive, Jargon, Linux Tips, Operating Systems, Solid State Drive.

Affiliate Disclosure: By buying the products we recommend, you help keep the site alive. Read more.

Whatsapp Pinterest

Leave a Reply

Your email address will not be published. Required fields are marked *

  1. kannon
    May 27, 2020 at 6:58 pm

    Really great article. I've been considering moving my ZFS pool's swap file to a dedicated partition on an SSD but didn't feel confident enough to do it. This article helped me understand swap a little better. Thanks!

  2. pranay
    October 13, 2016 at 7:12 am

    i've 8gb ram & 500gb hard disk with 5400rpm , do i need to create linux swap for dual boot with windows and ubuntu

  3. TSG
    September 10, 2016 at 10:51 am

    Great! Extraordinarily clear and informative! Thank you for this article! It helped me, I am going to remove swap partition to extend boot partition. Thank you!

  4. Leon
    June 27, 2016 at 1:22 pm

    Extraordinarily clear and informative! Thank you for this article!

  5. Anonymous
    November 4, 2015 at 3:11 am

    Thanks for this article, it helps a lot, since I've been using Ubuntu 14.04 from an 8gb flash drive, with 8gb RAM, this will help me save space.

  6. Anonymous
    June 7, 2015 at 8:49 am

    Very informative article. Thank you very much!!!

  7. Tshesko M
    May 15, 2015 at 9:03 pm

    Interesting Article
    I am about to switch off my computer and I have learnt something wich has strenghten my foundations

    Thanks to the previous comment, I now sure that I have been doing the right choice since the last past 2 years.

  8. Srivin Prabhash
    May 15, 2015 at 12:13 pm

    Greate Post .. TFS .. :)

  9. RedHat
    May 20, 2014 at 12:29 am

    A swap partition is simply the Linux equivalent of a Windows paging file. Many linux used/still can use a file rather than a swap partition. It's worth noting that unlike Windows which use only ram and a page file, some linux systems also have zram and zswap to aid the system. Zram takes a portion of ram an uses it as a swap partition. Zswap creates a compressed swap cache in ram for an existing swap partition.

  10. Robert B
    May 6, 2014 at 12:19 am

    Hi Danny,
    I liked your article was very informative, here is another really good one covering Swap partitions and also mentions the use of a Swap file and talks a little about how they relate and are different. One key point the article on Linux.com mentions is that if you do not have a lot of physical ram it is dangerous to run a system without either a Swap partition or a Swap file. The reason is if you run out of system ram then Linux will crash because it has no fall back position. Also be aware that you do not have exactly the amount of memory that you have installed available for your apps that are running because Linux divides ram memory into pages with the default page file being 4KB so when there is a memory request the Kernel allocates the number of 4KB pages to meet the demand, it also maintains a page file that relates directly to the physical ram address and the virtual memory request that the app is asking for. The page size can be modified as well depending on the type of applications that you run, there are large pages and huge pages; these optimize the performance of large data bases. I did not read up much on the large and huge pages so I do not not know if there are situations where they would be beneficial for the average desktop workstation but probably the default size is what every one should use. As you mentioned there are situations where having a swap partition actually slows things down but most systems are not net books with real limited system resources. If you are running a desktop workstation and do a lot of video or audio work having a large swap partition is very beneficial. One thing most people new to Linux are not aware of is that your various partitions do not have to reside on the same physical hard drive. The Linux install can be spread across several different drives, doing so will greatly speed up I/O because things can be written to one drive and read from another at the same time so there are fewer bottle necks(this is not as critical if all your drives are SSD's) A few years ago I ran a system that had a separate Home Partition, a Partition for the system files, (/opt /usr /etc /boot) and a separate Swap partition all on different hard drives. Having these on separate drives greatly increased the over all performance of my computer, granted this was a few years ago when PC's were not as fast as today. However it would still be advantageous to do this even today if you do a lot of video and audio recording and are striving for a low latency system. One thing that was explained to me about Swap a few years ago and do not know if it is accurate or not was that the Kernels use of Swap along with the file system chosen helped to maintain low file fragmentation on your hard drives. It temporarily stores info in Swap if there is not a lot of Ram before writing it out to permanent storage on the HD. It is not constantly writing data directly to the hard drive like Windows which will stick stuff where ever it finds a whole at the time of the write request so over time a Windows HD can become very fragmented. Have you ever heard of this before? I do not know if it is because of the FS design used as standard FS on Linux or a combination of both the FS along with the use of Swap. Do the FS's utilize Swap to organize data into blocks before it actually writes it to disk?
    Thanks for the good article,

    • Bruce E
      May 7, 2014 at 2:26 pm

      For a DAW or video editing station you should be looking at avoiding all unnecessary disk access. This includes swap. You are much better of having 16GB or more of RAM rather than having the system start swapping. You should also be using a low-latency kernel on those systems to get the best possible performance.

      When *nix filesystems look for space to store a file, they generally look for a contiguous chunk of drive space where the entire file will fit along with a bit of a buffer which is why file fragmentation is not as much of an issue as it is in Windows which looks for the first available space to write the file with each file immediately following the previous one. If there is not enough expansion room for a modified file to simply grow and remain contiguous where it currently sits and there is another contiguous space large enough to accommodate it, a *nix system will commonly move the entire file to the larger space. This means that the *nix filesystem will have free space scattered over the drive including between existing files to allow them to grow without needing to move them while most if not all files will be contiguous and the Windows system will have files fragmented all over the drive with the majority of its contiguous free space at the end of the drive in most use cases.

      I believe you were misled about using swap to organize anything before writing to disk. After all, writing to swap for this purpose IS writing to disk and using multiple disk I/O operations in this case just slows things down even more. That is just too much of a performance hit for a developer to contemplate.

  11. Tim Vels
    May 5, 2014 at 3:19 pm

    Wow I learned what Swappiness is about! :)
    Thanks!

  12. Scott H
    May 1, 2014 at 3:33 pm

    i alway partition memory cards if the device can only take a limited memory like some phone can not take 64gb of memory just partition it to 32 gb and then it reads 2 or make a shortcut from one partition to another and you got full memory out of a memory card very useful been doing it since i've been using linux and that 4 years ago but it only work with some device not all

  13. CJ
    May 1, 2014 at 3:13 pm

    My laptop has 8GB of RAM and I have an 8GB SWAP partition. Last week while editing photos and videos I was using over 7GB of RAM and 4GB of the SWAP partition was being used. I know a lot of people swear it's not needed but there most certainly situations where it is. With the prevalence of large hard drives, my storage drive is 1TB, setting aside a few gigabytes for swap is relatively painless.