0

A somewhat long-ish question this time: How can I ensure my PC uses the proper swapspace? I'm lucky enough to have an SSD disk where I have formatted a partition as Linux Swap. Problem is: it is not used.

This is what the system reports:

$ cat /proc/swaps
Filename            Type        Size        Used    Priority
/swapfile           file        2097148     0       -2

If I turn on my swap partition,

$ sudo swapon /dev/sdb5

I get:

$ cat /proc/swaps
Filename            Type        Size        Used    Priority
/swapfile           file        2097148     0       -2
/dev/sdb5           partition   16831484    0       -3

Then turning off the swapfile:

$ sudo swapoff /swapfile

$ cat /proc/swaps
Filename            Type        Size        Used    Priority
/dev/sdb5           partition   16831484    0       -2

This all works very well, but how can I make this stick also across a reboot?

I could add these commands to my .profile I guess, but I would like the system to use proper swap space also when someone else (potentially) logs in, i.e. as a system setting. Most likely I'll see a big improvement when the system is taxed... But how? I'd love to see the /swapfile gone, but I'm terrified I might end up with a system that won't boot :)

(As a sidenote - I know I should avoid using /dev/sdb5 and rather use the UUID – well, I'll get there eventually..!)

  • 2
    You have to edit your /etc/fstab file - this is what configures which file systems get mounted where, including swap. Most people prefer to use a swap file these days though, because it is more flexible. Any specific reason why you'd prefer a partition? – Byte Commander May 29 '19 at 21:14
  • 2
    New installs default to a swap file, but will use a swap partition. Most of the info on swap partitions is now old & outdated. You really do not want to use swap and probably do not need it if normal use and 4GB of RAM or more. help.ubuntu.com/community/SwapFaq Only if editing videos or have large databases in RAM may you need larger swap. Swap is orders of magnitude slower than RAM, so if you start using swap, you may want more RAM. – oldfred May 29 '19 at 21:20
  • The only reason is that I want the swap space to be on the quick SSD disk. I could of course mount the disk somewhere in my directory structure, but then I would have to move the swapfile to this directory. That's maybe easier? – Bernt Ribbum May 29 '19 at 21:25
  • Thanks for that info, @oldfred. My experience is 8GB of RAM and quite a bit of swapping, but that was a year or two ago. Things move quickly here! – Bernt Ribbum May 29 '19 at 21:28
  • My old system had 4GB of RAM and never used swap. But ordinary desktop use. What were you doing to use 8GB of RAM? RAM is normally full, as it caches activity as we often go back and run something again and then it is in RAM, but that is released if needed for a new application. – oldfred May 29 '19 at 21:37
1

Unused swap is normal. For example here's my memory usage - this is my laptop and it's been up for 7 hours today while I'm working:

$ free -h
              total        used        free      shared  buff/cache   available
Mem:           15Gi       3.2Gi       8.5Gi       1.5Gi       3.8Gi        10Gi
Swap:         979Mi          0B       979Mi

The fact is that you don't want to be actively using swap. Sometimes Linux will put unused programs in swap if there is enough memory pressure and that's fine. But if you are actually running out of memory you should really just add more RAM. Even if you are using an SSD swapping is slooow.

The only reason to add more swap is if you want to hibernate your machine. In that case you will need at least as much swap as you have RAM.

Anyway - if for some reason you are unable to add more RAM you need to put another line in your /etc/fstab for the new swap file. Something like this:

/dev/sdb5 none swap sw 0 0

It will now persist across reboots.

Side note:

If you want to see if your system is actively using swap use the dstat command and look at the paging column.

  • Thanks, montjoy! That turned out to be exactly the solution in fact :) – Bernt Ribbum May 29 '19 at 23:40
0

As it was pointed out by @Byte Commander, you have to edit the /etc/fstab file - this is what configures which file systems get mounted where, including swap.

All I had to do was to include the following line in my /etc/fstab file:

/dev/sdb5 none swap sw 0 0
  • if your problem was solved, please include the necessary steps in your answer so future readers can benefit from it. – danzel May 29 '19 at 23:25
  • Sure! The only reference to swap in my /etc/fstab is now this line: /dev/sdb5 none swap sw 0 0 – Bernt Ribbum May 29 '19 at 23:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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