2

For many years I set up my Linux machines with no swap, as they had enough memory to do what I needed and I would rather a process get killed if it used too much memory, instead of growing larger and larger and quietly slowing everything down.

However I found out I required swap in order to use hibernate on a laptop, so I created a swap partition and hibernate has been working fine.

Recently I found the machine was going into standby rather than hibernate, and upon investigation it turned out there was not enough space in the swap partition for hibernation to take place. This was because the swap partition I thought was reserved for hibernation, was in fact being used as normal swap space.

Is there some way I can tell Linux to use a given swap partition for hibernation only, and not to use it for swapping during normal operation?

EDIT: Per the question below, the machine has 8GB of memory and the swap partition is also 8GB, since I only wanted it for hibernation use and not actual swap use, so any larger than the machine's memory size would've been wasted. The underlying issue is that because the 8GB swap partition is being used as additional memory, the machine can now allocate up to 16GB of memory (8GB physical + 8GB swap). It recently had 10GB in use and of course could not hibernate as that 10GB could not fit in the 8GB swap partition.

  • What is the size of your physical RAM? What is the swap partition size? Hibernate requires a swap partition equal to the physical RAM size. Please click edit and let us know. Please do not use Add Comment; instead, use edit. – K7AAY Oct 28 '19 at 15:52
2

Is there some way I can tell Linux to use a given swap partition for hibernation only, and not to use it for swapping during normal operation?

Remove or comment the corresponding line from /etc/fstab. Example on my system

$ cat /etc/fstab |grep swap
/dev/mapper/NEO--L196--vg-swap_1 none            swap    sw              0       0

EDITED: OP is right, pm-hibernate needs a swap partition "activated" to work

So, the tips here is to keep the swap activated (so let it in /etc/fstab) but explicitly ask the kernel to ignore it.

This is done using the sysctl parameter vm.swappiness to 0 (valid values are 0-60, higher will make kernel swap more agressivly).

To ensure this setting is persistent over reboots, edit /etc/sysctl.conf and add a line vm.swappiness=0.

  • 1
    If you do that you can't hibernate at all because it says "insufficient swap space". – Malvineous Nov 1 '19 at 4:53
  • 2
    you're right, answer edited (thanks debian wiki...) – binarym Nov 3 '19 at 10:14
  • If I understand correctly, vm.swappiness=0 makes the kernel ignore all swap partitions, right? What if I want to have two swap partitions where one is reserved for "normal" swap and the other is reserved for hibernation? Then this answer wouldn't work, right? – Kal Nov 11 '19 at 14:09
  • 1
    @Kal You could script the hibernation process, setting vm.swappiness=0, adding the hibernation partition with swapon, activating hibernate, then after resume using swapoff for just the hibernation partition and restoring vm.swappiness. However this will likely still use the regular swap partition for hibernation data unless you disable it first with swapoff, which will cause all the regular swap to be read into memory and then written out again to the hibernation partition, slowing everything down. – Malvineous Nov 22 '19 at 23:26

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.