8

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

David Foerster
  • 36,264
  • 56
  • 94
  • 147
matanox
  • 2,293
  • 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. –  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? – matanox 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
  • hmmm I would regularly suspend even though it seems I have had no swap set up till now. Is hibernation identical to clicking "suspend" in the power menu, or is it actually something else? – matanox Jan 24 '18 at 15:09
  • 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. –  Jan 24 '18 at 15:23

1 Answers1

16

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.

matanox
  • 2,293
  • 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. – matanox Jan 24 '18 at 19:17