A service disappears without an error message. Nothing in its application log, no stop requested, and sometimes it comes back on its own a few seconds later. In most cases I handle, the kernel killed it because memory ran out. The kernel always says so, provided you look in the right place.
To write this article, I reproduced the situation on Ubuntu 26.04 (systemd 259): a process limited to 100 MB that asks for 300. The outputs below come from that test. The reading commands (parts 1 to 5) are the same on RHEL, Oracle Linux and Debian.
1. Read free correctly
free -h
total used free shared buff/cache available
Mem: 15Gi 3.1Gi 11Gi 40Mi 996Mi 12Gi
Swap: 4.0Gi 31Mi 4.0Gi
The column that matters is available, not free. Linux uses free memory as disk cache (buff/cache) and gives it back as soon as a program needs it. A server with 200 MB in free and 9 GB in available is fine. A server where available approaches zero and swap fills up is in trouble.
2. Find the kernel's trace
When memory is no longer enough, the kernel picks a process and kills it with the KILL signal. The process gets no chance to write to its log, which is why nothing shows on the application side. The trace is in the kernel log.
journalctl -k -g "out of memory|oom-killer" sudo dmesg -T | grep -i -E "out of memory|oom"
-k restricts to the kernel, -g filters by expression. After a reboot, add -b -1 to read the previous boot, provided the journal is kept on disk. Here is what I got:
kernel: python3 invoked oom-killer: gfp_mask=0xcc0(GFP_KERNEL), order=0, oom_score_adj=0
kernel: Memory cgroup out of memory: Killed process 1249738 (python3) total-vm:323248kB, anon-rss:102016kB, file-rss:3456kB, shmem-rss:0kB, UID:1000 pgtables:268kB oom_score_adj:0
Two lines to read:
invoked oom-killernames the process that asked for the memory that was not there. It is not always the one that gets killed.Killed processnames the victim, its PID and what it held.anon-rssis its own memory: here 102,016 kB, the 100 MB limit I had set.
3. The whole machine, or one service
The start of the Killed process line says who was short of memory:
Out of memory: Killed process: the whole machine ran out. Find what consumes memory, or add some.Memory cgroup out of memory: Killed process: a group went over its own limit while the machine still had room. This is a systemd service withMemoryMax=, or a container started with a limit.
In my test, the machine had 11 GB free. The group's limit triggered the kill. The oom-kill: line just before gives the group in oom_memcg=.
4. What systemd says about it
If the victim is a service, systemd knows and says it plainly:
systemctl status my-service journalctl -u my-service -n 20
demo-oom.service: The kernel OOM killer killed some processes in this unit.
demo-oom.service: Main process exited, code=killed, status=9/KILL
demo-oom.service: Failed with result 'oom-kill'.
demo-oom.service: Consumed 221ms CPU time over 285ms wall clock time, 100M memory peak.
Failed with result 'oom-kill' leaves no doubt. memory peak gives the maximum reached, useful to tell whether the limit is simply too low. To find every service that went through this:
journalctl -g "result 'oom-kill'" --since "-7d"
5. Why this one and not another
The kernel scores every process: the more memory it holds, the higher the score. It can be read in /proc:
cat /proc/1234/oom_score cat /proc/1234/oom_score_adj
oom_score_adj goes from -1000 to 1000 and shifts the score. At -1000, the process is never chosen. With systemd, this is set per service, without touching /proc:
[Service]
OOMScoreAdjust=-500
I use it to protect the database or sshd, not everything: if everything is protected, the kernel has no good choice left.
6. Limit a service before it takes the machine down
A leaking service ends up getting its neighbours killed. Giving it its own limit keeps the problem at home:
sudo systemctl edit my-service
[Service]
MemoryHigh=800M
MemoryMax=1G
Restart=on-failure
MemoryHigh slows the service down when it goes over this threshold, by reclaiming its memory first. MemoryMax is the hard limit: beyond it, the service is killed, and Restart=on-failure starts it again. Check what is really applied:
systemctl show my-service -p MemoryHigh -p MemoryMax
In my test, the command returned MemoryHigh=83886080 and MemoryMax=104857600, that is 80 MB and 100 MB in bytes.
On RHEL 8 and Oracle Linux 8, which still use the older group hierarchy (cgroup v1) by default, only MemoryMax= applies: MemoryHigh= needs cgroup v2, the default since RHEL 9. stat -fc %T /sys/fs/cgroup answers cgroup2fs when that is the case.
A limit does not fix a leak. It turns an outage of the whole machine into the restart of one service, which leaves time to find the cause.
The procedure, in short
free -h journalctl -k -g "out of memory|oom-killer" systemctl status my-service systemctl show my-service -p MemoryMax
Look at available, read the Killed process line, tell the machine from the group, then decide: more memory, a limit, or a fix in the application.