6

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.

Malvineous
  • 6,883
  • 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

1 Answers1

1

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

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

Deleted because pm-hibernate needs a swap partition "activated" to work

Keep the swap activated (so leave it alone 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-100; higher will make the kernel swap more aggressively; the default is 60).

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

binarym
  • 2,649
  • 1
    If you do that you can't hibernate at all because it says "insufficient swap space". – Malvineous Nov 01 '19 at 04:53
  • 2
    you're right, answer edited (thanks debian wiki...) – binarym Nov 03 '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
  • 2
    @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
  • 3
    Unless something has significant changed recently setting swapiness to 0 doesn't prevent the kernel using swap. See here: https://unix.stackexchange.com/questions/499485/how-do-i-use-swap-space-for-emergencies-only – Philip Couling Apr 19 '21 at 12:45