2

I run a rather plain Xubuntu bionic 18.04 install, with some extra user applications installed, but no extra systems software that I know of.

Now that I have extended my RAM to 16GB, I seem to have started using applications that use that much memory and more, much more. So I finally have regular use for swapping too. Instead of re-partitioning my disks, I added a swap file on the second disk that runs smoothly besides my swap partition on the first disk. The advantage is that work is spread over 2 disks, and I can feel the speed. In the /etc/fstab it looks like this:

LABEL=TSWP              none swap sw,pri=1,nofail
/media/sdata/.swapfile  none swap sw,pri=1,nofail

This results in typical swapon reports like this :

NAME                   TYPE      SIZE USED PRIO
/dev/sda2              partition  16G 4,8G    1
/media/sdata/.swapfile file       16G 4,7G    1

The application that causes so much swapping behaves very well with it (niced), and does not keep other applications from running smoothly. But it is one that often needs days or weeks to complete, and I would like to switch off my computer once in a while. Hibernation would be recommended.

Having ample swap space now, hibernating itself should be no problem. But where do I direct the kernel to, to resume from? There are loads of answers here (and on othe forums) that tell you how to do that for 1 swap location. But what if you have 2 locations?

  1. I could replace the swap file by a second swap partition on that same 2nd disk, and keep the advantage of spreading swapping workload, but that does not solve a thing: I still would not know which partition the hibernate image is written to, so I wouldn't be able to tell the kernel which swap partition to resume from.

  2. I could enlarge my original swap partition, and drop the swap file, but then I would loose the advantage of having 2 swapping locations. Also reorganising filled disks is risky business.

  3. I could differentiate the swap priority. If I give the swap file higher priority, and swapoff/swapon the swap partition, after some time the swapon report looks like this:

    NAME                   TYPE      SIZE USED PRIO
    /dev/sda2              partition  16G   0B    1
    /media/sdata/.swapfile file       16G 9,3G    2
    

    The swap partition in theory remains free for hibernation, as long as the swap file is large enough to handle all swapping. But not only do I loose the advantage of workload spread with this, it also seems probable that hibernation would follow the same priorities as swapping, and go for the used swap location instead of for the free one.

  4. I could add a pm-utils script to swapoff one of both swap locations as preparation for hibernate, leaving only the other location to choose from. But this would gravely diminish my chances of having enough unused swap space for the hibernation image, the opposite of what I obtained by adding a 2nd swap location.

  5. On the same level, I could mount/activate one swap location in /etc/fstab, and swapon the other in some startup script. Would that solve anything? Would the hibernation process look to /ect/fstab instead of to actual active swap locations? I guess not.

So my question is this: be it either 2 swap partitions, or 2 swap files, or 1 of each, how do I configure, or reliably predict, to which location the hibernation image will be written to? So that I can instruct the kernel to resume from that location, instead of seeking in vain on the other and loose the state of my long-running job.

A secondary question is: how would I go about finding the solution for my apparently special case myself. Does, for instance, systemctl hibernate call pm-hibernate of the pm-utils, is it the other way round, or is yet another agent finally responsible for the hibernation. Is any of these configurable with regards to swap location? Can I instead install another systems utility that is configurable in that way?

Thanks for your attention.

1 Answer 1

1

Just a few tidbits to consider...

In the best case scenerio, swap is not used at all. Use of swap is slow because of the speed of the devices that they're located on (HDD typically). The "I can feel the speed" is due to the additional RAM, not swap.

If you have a modern SSD, it's best to place one /swapfile on it, and to remove any swap partitions in use. Edits to /etc/fstab required.

If /etc/fstab, swap partitions are best referred to by UUID (UUID=xxx), rather than labels (LABEL=TSWP) or by device (/dev/sda2). Swapfiles are best placed in the root directory (/swapfile) rather than on removable media (/media/sdata/.swapfile), and not created as hidden.

In a non-hibernate situation, the swap size with 16G RAM might be in the 4-8G range. In a hibernate situation, the swap needs to be larger than physical RAM, in the order of (1.2-1.5)*RAM.

You should edit /etc/initramfs-tools/conf.d/resume to specify the resume swap... using your own UUID... like this...

RESUME=UUID=071f8b0e-8e16-4f4d-90ff-a4ae9cc56e2b

Then create a new /boot/initrd.img* file with:

sudo update-initramfs -c -k $(uname -r)

With 16G RAM, it's advantages to set vm.swappiness kernel parameter such that more RAM is used, and less swap is used.

To temporarily test this:

sudo sysctl vm.swappiness=10 # default is 60

To make a permanent change:

sudo sysctl -w vm.swappiness=10 # write value to /etc/sysctl.conf

sudo sysctl -p # load /etc/sysctl.conf

Lastly, after adding RAM, a memory test should be run to confirm good memory, and successful addition to memory slots. Go to https://www.memtest86.com/ and download/run their free memtest to test your memory. Get at least one complete pass of all the 4/4 tests to confirm good memory. This may take many hours to complete.

17
  • Thanks for the effort, but I must disagree on most points. - As I wrote, the order in time was add RAM, use new memory consuming programs, add extra swap location. Extra RAM was the enabler, extra swap location added speed
    – db-inf
    Dec 19, 2019 at 16:53
  • @db-inf Please specify...
    – heynnema
    Dec 19, 2019 at 16:53
  • Sorry, incomplete answer is published on every enter
    – db-inf
    Dec 19, 2019 at 16:56
  • @db-inf You're generally only allowed one question for each submission here on AU. Your question(s) were: should I use multiple swaps... and I said no. Your 2nd question was: configurable with regards to swap location... and you've already figured out the pri=n /etc/fstab configuration. I've added some info about the resume file for you. Rather than one-line comments, I'd much rather hear your expanded thoughts on what's wrong with what I suggest in my answer. I have a little experience in this area.
    – heynnema
    Dec 19, 2019 at 17:06
  • 1
    Sorry again, the short answer was an acident. Please allow me to take advantage of your willingness to help. My main question was not "should I use multiple swaps", nor did I ask for general advise on swapping: my swapping runs fine. Your answer about /etc/initramfs-tools/conf.d/resume is an interesting lead for further investigation, but does not yet answer my question: when using multiple swap locations, how do I configure, or reliably predict, to which location the hibernation image will be written to. That's the info I need to specify the resume location.
    – db-inf
    Dec 19, 2019 at 17:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .