5

This question already has an answer here:

I have noticed my system is not using any swap.

  • system got unresponsive when RAM got full
  • swapon --show returns nil
  • /etc/fstab has nothing about swap in it

I am utterly confused by the myriad of different ways to enable swap which I read about. Do I really need to define a swap file, rather than only point the system at a swap partition?

I have a partition of the right size formatted as linux-swap. What is the series of steps to be taken for (perpetually) enabling it in my OS?

Ubuntu 16.04

marked as duplicate by David Foerster, karel, Eric Carvalho, Thomas Ward Apr 24 '18 at 19:34

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    If you have ba swap partition already, add it to the fstab. The possibility of using a file instead of a partition isn't available for 16.04. – user692175 Jan 24 '18 at 14:31
  • 3
    "swap partition" -> difficult. "swap file" -> 1 command. By the way: if you are using a swap file you wont find it in fstab. @MichaelBay sudo fallocate -l 1G /swapfile && sudo mkswap /swapfile ? – Rinzwind Jan 24 '18 at 14:31
  • Is there any consensus on whether using a swap file is no longer a viable or recommended option as of 16.04 or otherwise? – matt Jan 24 '18 at 14:55
  • 1
    16.04 can use a swap file if you do not hibernate. The hibernate module requires a swap partition. You can even use both swap file and swap partition. If you hibernate, then your swap partition needs to be a few GB larger than your installed RAM. – ravery Jan 24 '18 at 15:02
  • 1
    Hibernation and suspension are different. The latter shuts down everything the RAM thus keeping your session "alive". The former saves the session (in swap) and shuts everything down. – user692175 Jan 24 '18 at 15:23
12

Well, I obtained the UUID of my idle linux-swap formatted partition from gparted, then:

  1. Made a backup of /etc/fstab
  2. Edited /etc/fstab to include the line: UUID=999990-999-999-9999-999 none swap sw 0 0 Of course you'll need to replace the 9's with your carefully determined UUID of your intended swap partition.
  3. sudo swapon --all --verbose

I think you really want to use the UUID even though you could go by partition name and stuff, because the names can easily change such as maybe when you add or reposition drives inside your machine.

The last command seems to correctly detect my swap partition, and htop now shows the size of the partition, hurray!

You may also want to get to know your swappiness value, which you can easily do following the swappiness section in these docs, while at it, even though you'd typically not find any surprises there nor have real reason to change it.

Even though this seems working, please do not follow these steps before few upvotes from other people have shown up aside my answer, I've only performed this once thus far.

  • this is the correct procedure – ravery Jan 24 '18 at 14:53
  • 2
    It might be better to tell people directly how they can view their swappiness value rather than (or as well as) linking to those docs. For reference you want sysctl vm.swappiness – ydaetskcoR Jan 24 '18 at 15:38
  • I upvoted your comment, but I think it's a long story and most times not necessary to fiddle with in the context here. – matt Jan 24 '18 at 19:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.