391. Out of memory
When a KDOS machine runs out of memory, kdos-oomd, KDOS’s out-of-memory daemon, ends the program that is starving it before the desktop stops responding. The kernel’s own out-of-memory killer remains behind it for the case the daemon does not catch. This page is for anyone whose program was closed without warning, or whose desktop froze under heavy memory use. It shows how to check that the daemon is running, how to find out what it killed and why, how to steer its choice, and how to turn it off. How it picks a victim is in the KDOS book’s kdos-oomd section; this page does not restate it.
Installation
The daemon is the kdos-oomd port, a program of KDOS’s own installed by 50_desktop as /usr/sbin/kdos-oomd. Its service script, /etc/init.d/57_oomd.sh, comes from the tree’s fs/ overlay. The image ships no oomd marker in /etc/service.disabled/, so it starts at every boot, on the live medium and on an installed system alike, under the supervisor ksvc.
The script skips the daemon, with a [SKIP] line, when the binary is missing or when the kernel’s pressure stall information (PSI) is off; see Services that skip themselves. The kernel is built with PSI on, in ports/core/boot/linux/kdos.config:
CONFIG_PSI=y
ports/core/boot/linux/kdos.config, lines 32–32
Its socket is read in the procedures below with socat, from the socat port, which the image carries.
Configuration
The daemon has no configuration file. What it watches for and what it never kills are compiled into src/daemons/kdos-oomd/main.c. KO_TRIGGER is written into /proc/pressure/memory as a stall amount in microseconds and the window it is measured over, KO_COOLDOWN is the shortest time between two scans, KO_AVAIL_PCT is the share of MemTotal that MemAvailable in /proc/meminfo must be at or under before a stall kills anything, KO_SWAP_PCT the share of SwapTotal that SwapFree at or under counts as exhausted, and KO_SWAP_AVAIL_PCT the higher share of MemTotal that applies instead while swap is exhausted, because a full zram swap leaves reclaim only the program files it reads straight back:
/* 150 ms of full stall in a 1 s window: every runnable thread stuck on memory
* for 15% of the last second. A desktop is already visibly hitching there. */
#define KO_TRIGGER "full 150000 1000000"
#define KO_COOLDOWN 10.0 /* seconds between victim scans, at least */
/* A kill also needs memory to be SHORT: MemAvailable at or under
* KO_AVAIL_PCT of MemTotal, or swap at or under KO_SWAP_PCT of SwapTotal
* with MemAvailable at or under KO_SWAP_AVAIL_PCT. See memory_short(). */
#define KO_AVAIL_PCT 10
#define KO_SWAP_PCT 2
#define KO_SWAP_AVAIL_PCT 25
src/daemons/kdos-oomd/main.c, lines 67–76
The programs it never chooses, matched by process name; every process whose name starts with pipewire is spared as well:
static const char *const ko_protected[] = {
"kwin_wayland", "kwin_wayland_wr", "plasmashell", "ksmserver", "kded6",
"plasma_session", "kglobalacceld", "kscreenlocker_g", "Xwayland",
"xdg-desktop-por", "polkit-kde-auth", "ksecretd",
"sddm", "sddm-helper", "sddm-greeter-qt", "elogind", "turnstiled",
"dbus-daemon", "ksvc", "kdos-powerd", "kdos-energyd", "kdos-oomd",
"kdos-settingsd", "accounts-daemon",
/*
* THE SESSION MANAGER IS NOT NAMED "pipewire" AND THE PREFIX BELOW
* DOES NOT REACH IT. wireplumber is where every routing decision
* lives — which sink a stream lands on, a headset's profile, the
* device list itself — so killing it leaves a running daemon with a
* graph nothing is connected to, which is a machine whose audio
* stopped for no reason a person can see.
*/
"wireplumber", NULL
};
src/daemons/kdos-oomd/main.c, lines 280–296
/etc/init.d/57_oomd.sh, from fs/etc/init.d/57_oomd.sh:
. /etc/init.d/service_helper
NAME="kdos-oomd"
DAEMON="/usr/sbin/kdos-oomd"
case "$1" in
start)
if [ ! -x "$DAEMON" ]; then
echo "[SKIP] $NAME: $DAEMON not found"
exit 0
fi
# PSI is what this daemon blocks on, and a kernel without it (psi=0 on
# the command line, or CONFIG_PSI off) cannot arm the trigger. The
# daemon refuses to start there rather than protecting nothing, and
# `supervise` would respawn a refusing daemon forever — so the check is
# here, before the respawn loop exists.
if [ ! -w /proc/pressure/memory ]; then
echo "[SKIP] $NAME: no writable /proc/pressure/memory (PSI off)"
exit 0
fi
echo "[KDOS] Starting $NAME..."
supervise "$NAME" "$DAEMON"
;;
stop)
stop_service "$NAME"
;;
status)
check_status "$NAME"
;;
*)
echo "Usage: $0 {start|stop|status}"
exit 1
;;
esac
Two settings outside the daemon change its choice:
- A box’s memory budget. The
memorykey of a box profile,~/.config/kdos/boxes/<box>.conf, such asmemory = 4G. A box whose processes together hold more than its budget is the first to lose one. The daemon reads the profile each time it chooses, from every/home/<user>/.config/kdos/boxes/, so an edited budget counts from the next choice with nothing restarted. The key is described in~/.config/kdos/boxes/<name>.conf. - A process’s
oom_score_adj. The daemon never chooses a process whose/proc/<pid>/oom_score_adjis -500 or lower. Lowering the value needs root, and a child inherits it, so a program that lowers its own shields everything it starts afterwards.
Usage
Checking that it is running
Ask the supervisor:
service status oomdOr run the KDOS check, which looks for the daemon’s socket:
kdos doctorIt prints
[ ok ] kdos-oomd watching /proc/pressure/memorywhen the daemon is up, and a[warn]line when it is not.
Finding out what it has done
Ask the daemon. The socket answers root, members of
wheel, and a user with an active elogind session on the seat, which the person at the desktop has:echo status | socat - UNIX-CONNECT:/run/kdos-oomd.sockThe answer is
ok trigger 'full 150000 1000000', kills 0, and after the first kill it ends withlast:and the victim, its box when it has one, its process id and its size, such askills 1, last: tail (pid 3801, 3344 MB). The count starts again at every start of the daemon.Read every kill since the system log started, each with the pressure that caused it:
sudo grep kdos-oomd /var/log/messagesA kill is logged as
kdos-oomd: killed <name> [(appbox <box>)] (pid <pid>, <size> MB) — memory pressure avg10 full=<f> some=<s>, <avail> of <total> MB available. For examplekdos-oomd: killed python3 (pid 3485, 1122 MB) — memory pressure avg10 full=1.17 some=1.23, 227 of 3892 MB available: a stall that fills a one-second window can be over before the ten-second average moves. The same lines are in/run/kdos-svc.kdos-oomd.log, which also holds them from beforesyslogdstarted; see Supervision and logging.Check whether the kernel’s own killer acted as well:
sudo dmesg | grep -i 'out of memory'
Watching memory pressure
cat /proc/pressure/memory
The full line is the share of time every runnable task was stalled on memory, averaged over 10, 60 and 300 seconds. The daemon acts when the full stall within one window of KO_TRIGGER reaches its amount and MemAvailable is at most KO_AVAIL_PCT percent of MemTotal, or at most KO_SWAP_AVAIL_PCT percent while SwapFree is at most KO_SWAP_PCT percent of SwapTotal:
grep -E 'MemTotal|MemAvailable|SwapTotal|SwapFree' /proc/meminfo
A single busy program, such as a large copy that recycles the page cache, can stall every task that wants to run while most of memory is still available; the daemon leaves it alone. avg10 is averaged over ten seconds, so a short stall that fires the trigger can read lower there.
Watching it act
A program that does nothing but take memory shows the daemon at work without risking anything else. tail keeps a whole line in memory, and /dev/zero never ends one:
timeout 180 tail /dev/zero
On a 4 GB machine with zram the shell reports Killed, exit status 137, long before timeout’s three minutes are up, after tail has grown to about 3.3 GB; dmesg shows no kill by the kernel, and the status query above then counts the kill. Save your work first: the desktop stalls while the memory fills.
Keeping one process out of its reach
Start the program with a lowered score:
sudo choom -n -500 -- <command>or lower the score of one already running:
sudo choom -n -500 -p <pid>choomis from util-linux. Runchoom -p <pid>to read a score back.Prefer a box budget for a boxed application: a budget makes the box the first candidate when it is over it, and leaves the rest of the session alone.
Turning it off
sudo service stop oomd
sudo service disable oomd
The machine then has only the kernel’s own killer. sudo service enable oomd and sudo service start oomd bring it back.
Tips
- A box’s budget is checked against the resident memory of all its processes added up, so a browser with many processes is measured as one.
- The daemon scans at most once per cool-down (
KO_COOLDOWNabove), counted from the moment a firing finds memory short and scans, whether or not that scan killed anything. A firing with memory not yet short starts no cool-down. A second program that keeps the machine stalled goes at the next scan, not at once. - The live medium keeps
/var/login RAM; a kill logged there is gone after power-off. Note it, or copy/var/log/messages, before shutting down. - Swap to zram gives the machine more room before the trigger fires; see zram and swap.
Troubleshooting
kdos doctor warns kdos-oomd not running. Read why it did not start:
cat /run/kdos-init.oomd.log
[SKIP] kdos-oomd: no writable /proc/pressure/memory (PSI off): PSI is switched off. Look forpsi=0in/proc/cmdlineand remove it from the boot entry.[SKIP] kdos-oomd: /usr/sbin/kdos-oomd not found: thekdos-oomdpackage is not installed.- No log: the service is disabled;
ls /etc/service.disabled/shows anoomdmarker.
Start it with sudo service start oomd, the command the doctor’s warning names.
socat prints err not permitted. The account is not in wheel and has no active session on the seat, as over SSH. Run it with sudo; kdos-oomd --explain <user> says which applies.
The desktop froze and no kill was logged. Check the count with the status query above, then the pressure: a machine whose full stall stays under KO_TRIGGER does not fire it, and one whose MemAvailable stays above KO_AVAIL_PCT percent of MemTotal (KO_SWAP_AVAIL_PCT percent with swap exhausted) kills nothing when it does. Read dmesg for a kill by the kernel. If a protected program is the one using the memory, the daemon finds nothing to kill; ps -eo rss,comm --sort=-rss | head shows the largest.
The wrong program was killed. The log line names it and its box. Give the box that should go first a memory budget, or lower the score of the process that should stay, as above. Which of several large processes the daemon picks is tested only against recorded process tables (see Known gaps).
A large copy fires the trigger. The installer’s Copy system step, and any copy of many gigabytes, streams through the page cache and can stall the machine long enough to fire KO_TRIGGER, most of all with little swap: the installer’s Prepare step turns off every swap area on a disk and leaves only zram. The page cache counts as available memory, so MemAvailable stays above a quarter of MemTotal and the daemon kills nothing, even with the zram swap full; the status query’s count stays where it was. The installer’s copy is out of reach even when memory does run short: kinstall sets its own score to -900 when an install starts, and rsync and every other program the install runs inherit it (cat /proc/$(pgrep -x rsync | head -1)/oom_score_adj during the Copy system step prints -900). Any other copy that must not be killed is started with a lowered score, as Keeping one process out of its reach under Usage shows.
Known gaps
- Security: the daemon’s choice between several large processes is tested only against recorded process tables.
- Status rates the daemon Beta and gives the one run in which it fired on a live machine.
See also
- kdos-oomd: how it decides, the socket and how it is tested
- Who may ask for what: which groups reach each root daemon
- Root daemons: the daemons that run as root
~/.config/kdos/boxes/<name>.conf: thememorykey- zram and swap: the compressed swap that comes before it
- Services: starting, stopping and disabling a service
- Logs: the system log and the
/runlogs