Pinterest Stumbleupon Whatsapp
Ads by Google

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

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

Overflow from Memory

linux_swap_ram
In it’s simplest sense, the SWAP partition acts as an overflow to your (RAM) memory. If your memory is filled up completely, any additional applications will be run off of the SWAP partition rather than memory.

This sounds like an easy way to increase the 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, extremely slow. The arrival of solid state drives have made the performance hit less of an issue with their much-improved speeds How Do Solid-State Drives Work? [MakeUseOf Explains] How Do Solid-State Drives Work? [MakeUseOf Explains] Over the past few decades, there has been a considerable amount of work in the field of computer hardware. While computer technology is constantly improving and evolving, rarely do we experience moments where we simply... Read More , but even they can’t match RAM – plus, you wouldn’t want to cause additional wear and tear on your solid state drive.

The closest analogy of the SWAP partition would be Windows’s pagefile, although there are many technical differences between the two.

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 ever touched would get moved to the SWAP partition.

Ads by Google

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 that items are more likely to be moved to the SWAP partition; a lower swappiness means that items are less likely to be moved to the SWAP partition.

Enables Hibernation

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

Of course, it’s actually quite rare for users to use the hibernation feature, so this may not matter to you.

Do You Need a SWAP Partition?

linux-swap-gparted
So, does this mean that a SWAP partition is necessary? Absolutely not! A Linux system can perform perfectly well without a SWAP partition. However, there are a few advantages and disadvantages of having one.

Advantages:

  • Provides overflow space when your memory fills up completely
  • Can move rarely-needed items away from your high-speed memory
  • Allows you to hibernate

Disadvantages:

  • Takes up space on your hard drive as SWAP partitions do not resize dynamically
  • Can increase wear and tear to your hard drive
  • Does not necessarily improve performance (see below)

When SWAP Partitions Don’t Help

What? SWAP partitions don’t always help improve performance? Let me explain a scenario where having a SWAP partition was actually worse than not having one.

I had Linux installed on a netbook that only had 1GB of memory and a 5400rpm hard drive. With only 1GB of memory, you can imagine that it can fill up pretty quickly with a few open browser tabs. The SWAP partition allowed me to keep them all open as the memory overflow simply went to it.

But then a bottleneck appeared, because of the hard drive’s 5400rpm speed. Because the hard drive was so slow, and the system constantly wanted to access the SWAP partition, the netbook became extremely, extremely sluggish to the point where it became virtually unusable unless I closed everything to free up some memory.

The set swappiness didn’t guarantee that, even though there was now space in the memory, everything in the SWAP partition would be moved back over. Instead, a lot of that would stay in the SWAP partition, causing the netbook to continue to be sluggish. This was only fixed by a reboot, which took a while anyway because the system had to remove everything from the SWAP partition before shutting down.

Recommendations

So, here’s what I would recommend:

  • 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.
  • If you 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, but it’s not needed unless you have less than 4GB of installed memory. 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 have a 5400rpm hard drive, then you shouldn’t create a SWAP partition simply because the bottleneck will make your computer worse off. However, if you absolutely want to have SWAP, then you can still create a partition using the same sizing guidelines outlined above – but change the swappiness value to something much lower.

Changing Swappiness

linux_swap_swappiness
To change the swappiness, you need to run the command gksu gedit /etc/sysctl.conf which will launch a text editor named Gedit, a fantastic all-around text editor gedit: One Of The Most Feature-Filled Plain Text Editors [Linux & Windows] gedit: One Of The Most Feature-Filled Plain Text Editors [Linux & Windows] When you think of plain text editors, the first thing that may pop into your head is Windows' Notepad application. It does exactly what its job description states - plain features for a plain text... Read More , for the configuration file we need to change. Next, locate “vm.swappiness” and change it to a different value (preferably 10). If you don’t see this parameter, add this line to the end of the file: vm.swappiness=10

The value you enter indicates when you want Linux to start actively moving processes from the memory to the SWAP partition. So for example, a value of 10 indicates that processes will be moved when memory usage reaches 90%; the default swappiness value in Ubuntu of 60 indicates that processes will be moved when memory usage reaches 40%.

There are a lot of other details that go into this, but they would only make things more confusing.

Conclusion

SWAP partitions can make a major difference in your system’s performance – sometimes for better and sometimes for worse. Now that you’re well-educated, you should be able to make the right decisions.

Looking for other ways to speed up your Linux system? Check out these four other quick and easy tips 4 Ways to Speed Up Your Linux PC 4 Ways to Speed Up Your Linux PC Is your Linux setup not as speedy as you'd like? Here's how to speed it up. Read More .

What have you heard about SWAP partitions? Do you think they’re worth using? Let us know in the comments!

Image Credits: Daniel Rocal

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

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

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

    Extraordinarily clear and informative! Thank you for this article!

  4. Andrew Speidel
    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.

  5. Georgiou A. George
    June 7, 2015 at 8:49 am

    Very informative article. Thank you very much!!!

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

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

    Greate Post .. TFS .. :)

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

  9. 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. http://www.linux.com/news/software/applications/8208-all-about-linux-swap-space 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.

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

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

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

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

Leave a Reply

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