Skip to main content

Linux and servers · Intermediate · 15 min

Find what is using memory and read OOM killer messages

By Padding · Updated 18 Nov 2026
Ubuntu 26.04.1 LTS · procps 4.0.4 · smem 1.5 · systemd 259

You will read free and /proc/meminfo correctly, rank processes by RSS and PSS, decode an "Out of memory Killed process" line from the kernel log, and set OOMScoreAdjust= and MemoryMax= so the right service goes first.

On this page

A server that "ran out of memory" usually did one of two things: it slowed to a crawl while it swapped, or the kernel's out-of-memory (OOM) killer picked a process and stopped it with SIGKILL. The second one is quiet. Your app or database is simply gone, systemd may restart it, and the only record is a few lines in the kernel log.

This tutorial gives you a routine for both halves of the problem. First you read the memory numbers the way the kernel means them, then you find which process holds the memory and how much of it is really its own. Then you read the OOM killer's report line by line, and finally you tell the kernel which service to sacrifice first and cap the one that misbehaves.

You will use a small Python script that holds a fixed amount of memory, so you can watch every number move by a known amount. On a 1 GB or 2 GB VPS, the size many teams start on to keep a monthly bill predictable, these readings are the difference between buying a bigger plan and fixing the one process that grew.

Step 1 · Read free and /proc/meminfo the way the kernel means them

Install smem, which you will use later, and look at the summary first:

Bash
sudo apt install -y smem python3
free -h

free -h prints a Mem: line and a Swap: line. The column that answers "how much memory can a new program get?" is available, not free. Linux keeps file contents it has read recently in RAM (the page cache, shown under buff/cache) because unused RAM does nothing. When a program asks for memory, the kernel drops clean cache pages and hands them over. So a healthy server often shows free near zero and available large. Worry when available is low, not when free is.

free reads its numbers from /proc/meminfo. These are the lines worth knowing:

Bash
grep -E '^(MemTotal|MemFree|MemAvailable|Buffers|Cached|AnonPages|Shmem|SwapTotal|SwapFree|Committed_AS):' /proc/meminfo
  • MemTotal: usable RAM, slightly less than the plan size because the kernel reserves some.
  • MemAvailable: the kernel's estimate of what can be given to new work without swapping. This is the available column.
  • Cached and Buffers: page cache and block device buffers. Most of it can be reclaimed, but not all.
  • AnonPages: memory that programs allocated for themselves (heaps, stacks). It has no file behind it, so it can only be freed by the program or moved to swap.
  • Shmem: shared memory and files in tmpfs such as /dev/shm and /run. It counts towards Cached but cannot be dropped like normal cache.
  • SwapTotal and SwapFree: swap size and what is left.
  • Committed_AS: how much memory all processes have asked for, used or not. When it is far above MemTotal plus swap, the server is betting that programs will not use all they reserved.

Check it worked

free -h shows an available value, and you can explain why it is larger than free. The grep prints ten lines, each ending in kB. Write down MemAvailable now; you will compare it in the next step.

Step 2 · Start a process that holds a known amount of memory

Create a demo folder and a script that allocates a block of memory, writes to every page of it so it is really in RAM, and then waits:

Bash
mkdir -p ~/memory-demo
cat > ~/memory-demo/hog.py <<'EOF'
import os, sys, time

mb = int(sys.argv[1]) if len(sys.argv) > 1 else 200
# Multiplying a bytes object writes every byte, so every page is resident.
blob = b"\x01" * (mb * 1024 * 1024)
if "--fork" in sys.argv:
    os.fork()  # parent and child now share the same pages
print(f"pid {os.getpid()} holding {mb} MB", flush=True)
time.sleep(3600)
EOF

Run it in the background with 200 MB, sending its output to a log file:

Bash
python3 ~/memory-demo/hog.py 200 > ~/memory-demo/hog.log 2>&1 &

Give it a second, then read the log and the numbers again:

Bash
cat ~/memory-demo/hog.log
free -h
grep -E '^(MemAvailable|AnonPages):' /proc/meminfo

Check it worked

The log says pid <number> holding 200 MB. Compared with the previous step, MemAvailable has dropped and AnonPages has risen by roughly 200 MB (about 204,800 kB). Other programs come and go, so the change is close to 200 MB rather than exact. If the numbers did not move, check the process is still running with pgrep -af hog.py.

Step 3 · Rank processes by resident memory with ps

Each process has two sizes. VSZ is the virtual address space it has mapped, which includes memory it reserved but never touched, so it is often huge and rarely useful. RSS (resident set size) is the RAM its pages occupy right now. Sort by RSS:

Bash
ps -eo pid,user,rss,vsz,comm --sort=-rss | head -n 8

rss and vsz are in kilobytes. To see the same thing live, run top -o %MEM, which sorts by memory share; press q to leave.

The kernel also keeps a per-process summary in /proc/<pid>/status. Look at the demo process:

Bash
grep -E '^(VmRSS|RssAnon|RssFile|RssShmem):' /proc/$(pgrep -f memory-demo/hog.py)/status

RssAnon is the program's own allocations, RssFile is pages of files it has mapped (its binary and libraries), and RssShmem is shared memory.

Check it worked

The demo python3 process is at or near the top of the ps list with an RSS of a little over 200,000 kB, and its RssAnon line is close to 204800 kB. On your own server, the processes above it are the ones to look at first.

Step 4 · Measure shared memory fairly with PSS

RSS counts every page a process can see, including pages it shares with other processes. A web server with ten worker processes that share most of their code shows ten large RSS numbers, and adding them up gives a total far bigger than the RAM really used. PSS (proportional set size) fixes this: a page shared by two processes counts half to each.

Restart the demo so the memory is shared. With --fork, the script allocates 200 MB and then forks, so a parent and a child point at the same 200 MB:

Bash
pkill -f memory-demo/hog.py
python3 ~/memory-demo/hog.py 200 --fork > ~/memory-demo/hog.log 2>&1 &

Read RSS and PSS for each of the two processes from smaps_rollup, the kernel's per-process total of every mapping:

Bash
for pid in $(pgrep -f memory-demo/hog.py); do
  echo "== $pid"
  grep -E '^(Rss|Pss|Shared_Dirty|Private_Dirty):' /proc/$pid/smaps_rollup
done

smem does the same sum for every process and can total it. The [h]og pattern stops smem from matching its own command line, which also contains the search text:

Bash
sudo smem -k -t -c "pid user command uss pss rss" -P '[h]og.py'

USS (unique set size) is memory only that process uses: what you would get back if you killed it alone. Run sudo smem -k -s pss -r | head -n 10 on your own server to list the ten biggest processes by PSS.

Check it worked

Each of the two processes shows Rss of about 205,000 kB but Pss of roughly half that, and most of its memory sits under Shared_Dirty. In the smem totals line, the RSS column adds up to about 400 MB while PSS adds up to about 200 MB, which is the RAM really in use. When processes share memory, sum PSS, not RSS.

Step 5 · Read an OOM killer report in the kernel log

When the kernel cannot free enough memory for an allocation, it picks one process, kills it, and writes a report to the kernel log. Search the kernel messages from the current boot:

Bash
sudo journalctl -k --grep 'out of memory|oom-kill'

--grep is case-insensitive when the pattern is all lower case, so this catches both the global and the cgroup form of the message. If the server rebooted after the incident, add -b -1 to search the previous boot; that only works if the journal is kept on disk (see /tutorials/keep-the-systemd-journal-small-and-persistent). sudo dmesg -T | grep -i 'out of memory' reads the same messages from the kernel's ring buffer for the current boot.

A report looks something like this (the numbers are an example):

Text
python3 invoked oom-killer: gfp_mask=0x140cca(GFP_HIGHUSER_MOVABLE|__GFP_COMP), order=0, oom_score_adj=0
...
oom-kill:constraint=CONSTRAINT_NONE,nodemask=(null),cpuset=/,mems_allowed=0,global_oom,task_memcg=/system.slice/myapp.service,task=python3,pid=4242,uid=998
Out of memory: Killed process 4242 (python3) total-vm:1258300kB, anon-rss:812340kB, file-rss:2048kB, shmem-rss:0kB, UID:998 pgtables:1720kB oom_score_adj:0

Read it from the bottom up:

  • Killed process 4242 (python3) is the victim: its PID and command name.
  • anon-rss:812340kB is the memory the victim had allocated for itself, about 790 MB here. That is what the kill gave back. total-vm is its virtual size and says little.
  • UID:998 is the user it ran as. Look it up with getent passwd 998.
  • oom_score_adj:0 is the adjustment it carried; you will set this in the next steps.
  • task_memcg=/system.slice/myapp.service names the cgroup, which for a systemd service is the unit. This tells you which service died even when the command name is a generic python3 or java.
  • constraint=CONSTRAINT_NONE with global_oom means the whole server ran out. CONSTRAINT_MEMCG, and a line starting Memory cgroup out of memory, mean a memory limit on that cgroup was hit while the server as a whole may have had plenty free.
  • The first line names the process that asked for memory when none was left. It is often not the one that was killed.

Between the first and last lines the kernel prints a memory summary and a table of every process with its rss and oom_score_adj. That table shows what else was large at the moment of the kill.

systemd notices too. For a service, its own log has lines something like myapp.service: A process of this unit has been killed by the OOM killer. and Failed with result 'oom-kill':

Bash
sudo journalctl -u myapp --since today | grep -i oom

Check it worked

You can take any Killed process line from your server, or the example above, and say which service died, how much memory it held (from anon-rss), and whether the whole server or one cgroup limit ran out (from constraint=). If the search prints -- No entries --, the OOM killer has not run during the period the journal covers.

Step 6 · See how the kernel chooses its victim

The kernel kills the process with the highest badness. Internally, badness is the process's RAM and swap use, plus oom_score_adj scaled to total memory (an adjustment of 1000 counts as all of RAM). /proc/<pid>/oom_score shows that badness rescaled to a 0 to 2000 range and then multiplied by two thirds, so an ordinary process with no adjustment reads around 666 or a little more, and a bigger process reads higher. Read both for the demo parent:

Bash
pid=$(pgrep -o -f memory-demo/hog.py)
cat /proc/$pid/oom_score /proc/$pid/oom_score_adj

oom_score_adj runs from -1000 to 1000. A positive value makes the process a likelier victim, a negative one protects it, and -1000 means "never kill this". Any user can raise the value on their own processes; lowering it needs root. Mark the demo process as the first to go:

Bash
echo 500 | sudo tee /proc/$pid/oom_score_adj
cat /proc/$pid/oom_score

A value written to /proc lasts only as long as that process. For a service, set it in the unit, which the next step does.

Check it worked

The first cat prints a score and 0. After tee, the score is about 333 higher than before (the /proc value is scaled by two thirds). On your own server, list the likeliest victims with for p in /proc/[0-9]*; do echo "$(cat $p/oom_score 2>/dev/null) $(cat $p/comm 2>/dev/null)"; done | sort -rn | head.

Step 7 · Set OOMScoreAdjust= and MemoryMax= for one service

Two settings in a systemd unit change what happens when memory runs out:

  • OOMScoreAdjust= sets oom_score_adj on every process the service starts. Give a negative value such as -500 to a service you must keep (your database) and a positive value to one you can lose and restart (a batch worker). Leave -1000 for the very few processes, such as sshd, that you need to log in and repair things.
  • MemoryMax= is a hard cap on the service's cgroup. When the service reaches it, the kernel reclaims and then OOM-kills inside that service only, so one leaking app cannot take the database down with it. Ubuntu 26.04 uses cgroup v2, where this works out of the box.

This example uses the myapp service from /tutorials/run-an-app-as-a-systemd-service; replace myapp with your own service name. Put the settings in a drop-in, so a package upgrade or redeploy of the main unit does not remove them:

Bash
sudo mkdir -p /etc/systemd/system/myapp.service.d
sudo tee /etc/systemd/system/myapp.service.d/memory.conf > /dev/null <<'EOF'
[Service]
MemoryHigh=250M
MemoryMax=300M
OOMScoreAdjust=200
EOF
sudo systemd-analyze verify /etc/systemd/system/myapp.service

MemoryHigh= sits below the cap: above it the kernel throttles the service and reclaims its memory hard, which slows it before it reaches MemoryMax= and gets killed. Pick values from what you measured: the service's normal PSS plus room for peaks. /tutorials/limit-a-service-cpu-and-memory-with-systemd covers these limits, CPU and process counts in depth.

Apply it and check that systemd has read the values:

Bash
sudo systemctl daemon-reload
sudo systemctl restart myapp
systemctl show myapp -p MemoryHigh -p MemoryMax -p OOMScoreAdjust -p OOMPolicy
systemctl status myapp

By default, when the kernel OOM-kills one process of a service, systemd stops the rest of the unit too (OOMPolicy=stop) and records the result as oom-kill. With Restart=on-failure in the unit, systemd then starts it again.

Check it worked

systemd-analyze verify prints nothing about memory.conf; a typo in a setting name shows up as Unknown key name. After the restart, systemctl show prints MemoryMax=314572800 (300 MiB in bytes), OOMScoreAdjust=200 and OOMPolicy=stop, and the Memory: line in systemctl status myapp shows current use with the high and max limits beside it. cat /proc/$(systemctl show -p MainPID --value myapp)/oom_score_adj prints 200.

Step 8 · Check whether systemd-oomd is watching

The kernel OOM killer acts only when memory has completely run out, which can be after minutes of a server so busy reclaiming memory that it barely answers. systemd-oomd is a userspace killer that acts earlier. It watches memory pressure (how much time tasks spend stalled waiting for memory) and swap use per cgroup, and kills a whole cgroup that has opted in when pressure stays above a limit. Ubuntu Desktop has it enabled since 22.04; Ubuntu Server does not install it by default. Check your server:

Bash
systemctl status systemd-oomd
oomctl

If systemctl status reports Unit systemd-oomd.service could not be found, it is not installed and only the kernel OOM killer protects the server. That is a reasonable default for a server. If you install it (sudo apt install systemd-oomd), units opt in with ManagedOOMMemoryPressure=kill, and it works best with some swap configured; /tutorials/add-swap-space-with-a-swap-file shows how. When it does kill something, its messages appear under journalctl -u systemd-oomd, not in the kernel log, so search there as well.

Check it worked

You know which killers run on your server: the kernel OOM killer always, and systemd-oomd only if systemctl status systemd-oomd shows active (running). If it is running, oomctl lists the cgroups it monitors and their current pressure.

Step 9 · Clean up the demo

Stop both demo processes and remove the folder:

Bash
pkill -f memory-demo/hog.py
rm -rf ~/memory-demo

Keep the memory.conf drop-in if you want the limits on your service. If you added it only to try it, remove it and reload:

Bash
sudo rm /etc/systemd/system/myapp.service.d/memory.conf
Bash
sudo systemctl daemon-reload
sudo systemctl restart myapp

Check it worked

pgrep -af hog.py prints nothing, ls ~/memory-demo reports No such file or directory, and free -h shows available back near the value you wrote down in the first step. If you removed the drop-in, systemctl show myapp -p MemoryMax prints MemoryMax=infinity.

Next steps

  • Look at CPU, disk and load alongside memory with /tutorials/find-why-a-linux-server-is-slow.
  • Give short memory peaks somewhere to go with /tutorials/add-swap-space-with-a-swap-file.
  • Set memory, CPU and process limits for a service in full with /tutorials/limit-a-service-cpu-and-memory-with-systemd.

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.