Swapfile Usage

JCCHOME Linux uses ZRAM swap by default. It’s still a good idea to have a backup swapfile on disk. In addition, it’s usually best if swap is used as an absolute last resort.  But, we don’t want any OOM, especially on production systems.

So, future releases will proceed with the following default scheme:

We start with the default settings for ZRAM

/etc/default/zramswap

# Specifies amount of zram devices to create.
# By default, zramswap-start will use all available cores.
#CORES=1

# Specifies the amount of RAM that should be used for zram
# based on a percentage the total amount of available memory
PERCENTAGE=50

# Specifies a static amount of RAM that should be used for
# the ZRAM devices, this is in MiB
#ALLOCATION=8192

# Specifies the priority for the swap devices, see swapon(2)
# for more details.
PRIORITY=100

This allows for a very fast amount of zram which is always available.

But, we still have to create a backup swapfile.  ZRAM is great, but we can’t guarantee it’ll provide enough swap space in a pinch with systems with small amounts of RAM.

So, we will also create a local swap file.  This will easily (And automatically) be done from the JCCHOME menu:

WE USE BTRFS SO WE HAVE TO ACCOUNT FOR CoW:

mkdir /swap

chattr -R +C /swap

touch /swap/swapfile0

fallocate -l 8G /swap/swapfile0

USE WHATEVER SIZE YOU NEED HERE

As was pointed out to me here, it’s a very good idea to make the directory permissions 700 and the file permissions 600:

    chmod 0700 /swap

    chmod 0600 /swap/swapfile0

NOW THAT THE FILE IS SECURE, WE CAN FORMAT THE SWAP FILE

mkswap /swap/swapfile0

NOW WE NEED TO ADD THE FILE TO OUR FSTAB (CAN’T USE UUID)

echo “/swap/swapfile0 none swap sw,pri=10 0 0” >> /etc/fstab

swapon -a

NOW WE CAN SEE THE SWAP SPACE:

cat /proc/swaps

Filename Type Size Used Priority
/swap/swapfile file 8388604 0 10
/dev/zram0 partition 32874572 0 100

REMEMBER THAT THESE NUMBERS ARE MEASURED IN KILOBYTES!

The last thing to do, which will also be default going forward, is to change the swappiness of the system.

Edit /etc/sysctl.conf

vm.swappiness=1

Now we execute sysctl -p

This will make the system only use swap if necessary

This is based on the following rule here: https://en.wikipedia.org/wiki/Memory_paging#Swappiness

vm.swappiness = 0   # Swap is disabled. In earlier versions, this meant that the kernel would swap only to avoid an out of memory condition, but in later versions this is achieved by setting to 1.
vm.swappiness = 1   # Kernel version 3.5 and over, as well as kernel version 2.6.32-303 and over: Minimum amount of swapping without disabling it entirely.
vm.swappiness = 10  # This value is sometimes recommended to improve performance when sufficient memory exists in a system, this value *10* could be considered for the performance being expected. 
vm.swappiness = 60  # The default value.
vm.swappiness = 100 # The kernel will swap aggressively.

These default settings seem to work well across many different hardware configurations

Whether you have a small amount of RAM, a mechanical hard drive that can’t handle random writes well, and/or really old processors/memory, these settings seem to help keep the system running well, and avoid OOM errors.  This has been testing now on workstations, docker hosts, and KVM servers.