brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: swapon -p decides which swap area Linux fills first

Linux
~/tips/tip-memory-swapon-priority-pri

sudo swapon -p 10 /swap-fast

Sets a priority per swap area, so a fast one is used before a slow one.

With two swap areas, Linux fills the older one first unless you say otherwise. If one is fast and one is slow, I want the fast one used first. A priority does that.

sudo swapon -p 10 /swap-fast
sudo swapon -p 5 /swap-slow
swapon --show=TYPE,SIZE,USED,PRIO

The priority is a number from 0 to 32767, and a higher number means the area is used first. With two small test swap files, one at priority 10 and one at 5, the table looked like this:

TYPE SIZE USED PRIO
file   4M   0B   10
file   4M   0B    5

The swapon(2) man page explains the rules. Pages are allocated in priority order, and a higher-priority area is used up before the next one is touched. Areas that share the same, highest priority are used round-robin. If you set nothing, the kernel hands out negative numbers, and newer areas get an even lower priority than older ones.

To make it permanent, put pri= in the options field of /etc/fstab. Here the swap file path is made up:

/swap-fast  none  swap  sw,pri=10  0  0

The man page says pri= is used with swapon -a. If you pair a compressed zram device with a swap area on disk, give the zram device the higher number. I’d keep the numbers simple, like 100 and 10, so the order is obvious next time you read the file.

One caveat: the priority only decides the order, not the size. A high-priority area that is small fills up first, and then the kernel moves on to the next one.

Further reading: swapon(8) man page, swapon(2) man page

More Linux tips a2query -m rewrite renice -n 10 -p 4242 cat config.yml | xsel -ib xvfb-run -a your-program --some-flag dpkg --get-selections > packages.txt All Linux tips (261) All topics

Link