Linux Basics Lesson 26 of 38
Who eats the memory
10 minutes
Learning goals
- List processes by memory with ps aux --sort=-%mem and tell RSS from VSZ.
- Explain what the OOM killer does and when it strikes.
- Prove a kill from the kernel log with journalctl -k, and see the service's view with journalctl -u.
- Say why dmesg fails without root on Debian, and what the usual way is on a real server.
ps: who has the memory
The same ps aux as in Module 6, sorted by memory this time:
ps aux --sort=-%mem
Purpose: Lists the processes with the biggest memory users first.
Syntax: ps aux --sort=-%mem | ps aux --sort=-rss
Options:
Example:
ps aux --sort=-%mem
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
app 5120 2.1 33.3 7340032 5242880 ? Ss 06:02 0:00 python3 app.p
app 2210 0.4 1.3 12582912 204800 ? Ss 06:00 0:00 java -jar rep
root 1 0.0 0.0 22416 13056 ? Ss Oct05 0:02 /sbin/init
lerner 4242 0.0 0.0 8608 5120 pts/0 Ss 09:29 0:00 bash
lerner 5121 0.0 0.0 6788 4136 pts/0 R+ 09:30 0:00 ps aux --sortps cuts its lines at the width of the terminal, here at 80 characters, so you read python3 app.p and not the whole name. The PID is what you hold on to.
On a real server with hundreds of processes, the usual grip is ps aux --sort=-%mem | head. head keeps only the first ten lines, the header and the biggest consumers. head is not loaded on this page, but with five processes you see everything anyway.
Two columns look alike and mean very different things. RSS is the memory that a process actually occupies in RAM. VSZ is the address space that it has reserved. Look at the Java process: more than 12 GB under VSZ, but only 200 MB under RSS. With --sort=-vsz it would be at the top, and you would be chasing the wrong process.
The OOM killer
OOM stands for out of memory. If RAM and swap are both used up and a program wants more, the kernel has no good choice left. To keep the machine alive, it picks a process, usually the one that frees the most, and kills it with SIGKILL. The process gets no chance to write anything, which is why its own log simply stops. The only thing left is the entry that the kernel writes itself.
There are two ways to read it. journalctl -k shows the kernel messages from the journal. It works here without sudo, because lerner is in the group adm. With -p err only the errors remain:
journalctl -k -p err --no-pager
Purpose: Shows the error messages of the kernel, where a kill by the OOM killer stands.
Syntax: journalctl -k [-p err] [--no-pager] | journalctl -u UNIT
Options:
Example:
journalctl -k -p err --no-pager
Oct 17 03:12:07 web-01 kernel: Out of memory: Killed process 4711 (python3) total-vm:7864320kB, anon-rss:6291456kB, file-rss:1024kB, shmem-rss:0kB, UID:998 pgtables:13000kB oom_score_adj:0That is the proof: Killed process 4711, with the name and the memory it had. --no-pager prints everything at once, so that you can also read it on a phone. Without -p err you also see the line before it, python3 invoked oom-killer, naming the process that asked for memory at the wrong moment. That does not have to be the one that dies.
The other way is dmesg, and it fails here:
dmesg
dmesg: read kernel buffer failed: Operation not permitted
Debian reserves dmesg for root (kernel.dmesg_restrict=1), other distributions, RHEL and Rocky among them, may allow it. On a real server, sudo dmesg | grep -i oom is the usual way to go. On this page it does not run: sudo answers with a hint, and grep with command not found. That is due to this page and not to the command, and you will need that line in real life. It finds the kill line through oom_score_adj, by the way.
The service's view is shown by journalctl -u:
journalctl -u app --no-pager
Oct 17 03:12:08 web-01 systemd[1]: app.service: A process of this unit has been killed by the OOM killer.
Oct 17 03:12:08 web-01 systemd[1]: app.service: Failed with result 'oom-kill'.
Oct 17 06:02:04 web-01 systemd[1]: Started app.service - Shop API.
A second witness, then, with the restart nearly three hours later. On a real server, systemctl status app shows such a result as well. systemctl is not loaded here and answers with a hint.
Practice
Terminal task 1/3
Show all processes so that the one occupying the most memory is at the top.
Task 1/3: Show all processes so that the one occupying the most memory is at the top.
Show all processes so that the one occupying the most memory is at the top.
Task complete
Solution
ps aux --sort=-%mem Why: python3 app.py has 33 % of the RAM. With --sort=-vsz the Java process would be at the top, even though it occupies far less.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Terminal task 2/3
Mission
In the night, the app disappeared. Prove with the kernel log that the OOM killer did it. You are looking for the line Killed process 4711.
Task 2/3: In the night, the app disappeared. Prove with the kernel log that the OOM killer did it. You are looking for the line Killed process 4711.
Mission
In the night, the app disappeared. Prove with the kernel log that the OOM killer did it. You are looking for the line Killed process 4711.
Task complete
Solution
journalctl -k --no-pager Why: That is a proof, not a guess: the kernel names the PID, the program and how much memory it had. On a real server, sudo dmesg | grep -i oom gets you to the same line.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Terminal task 3/3
Now the view of the service. Show what the journal says about app, and find the result of the failure.
Task 3/3: Now the view of the service. Show what the journal says about app, and find the result of the failure.
Now the view of the service. Show what the journal says about app, and find the result of the failure.
Task complete
Solution
journalctl -u app --no-pager Why: Two independent witnesses, the kernel and systemd, say the same thing. On a real server, systemctl status app shows this result too.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Fill the gap
The memory that a process really occupies in RAM is in the column:
The memory that a process really occupies in RAM is in the column: …
Check works once you have typed an answer.
Predict the output
Command
dmesgBefore the command, in your home folder ~
- ~/.bashrc
- ~/.profile
- ~/.secret
- ~/Documents/
- ~/logs/
- ~/notes.txt
- ~/projects/
- ~/archive/
Check works once you have chosen an answer.
Put in order
A service disappeared during the night. Put the steps in order.
Steps
- Read the kernel log and look for the line Killed process.
- Write the finding into the ticket, with the kernel line.
- Look at the situation now with free and ps sorted by memory.
- Address the cause, with a memory limit or a fix in the app.
- Check the service's view with journalctl -u.
Next up: 03:12, and this time you are the one who has to prove it.
Quiz
-
Check works once you have chosen an answer.
-
Check works once you have chosen an answer.
-
Check works once you have chosen an answer.
Cards
-
Card 1 of 3
Tap the card or press Space to turn it over.
-
Card 2 of 3
Tap the card or press Space to turn it over.
-
Card 3 of 3
Tap the card or press Space to turn it over.
End of the lesson
Still open: 6 exercises, 3 quiz questions
What you can do now: ps aux --sort=-%mem, journalctl -k -p err --no-pager
+0 XP in this lesson
- Lesson 1: open
- Lesson 2: open
- Lesson 3: open
- Lesson 4: open
- Lesson 5: open
- Lesson 6: open
- Lesson 7: open
- Lesson 8: open
- Lesson 9: open
- Lesson 10: open
- Lesson 11: open
- Lesson 12: open
- Lesson 13: open
- Lesson 14: open
- Lesson 15: open
- Lesson 16: open
- Lesson 17: open
- Lesson 18: open
- Lesson 19: open
- Lesson 20: open
- Lesson 21: open
- Lesson 22: open
- Lesson 23: open
- Lesson 24: open
- Lesson 25: open
- Lesson 26: done
- Lesson 27: open
- Lesson 28: open
- Lesson 29: open
- Lesson 30: open
- Lesson 31: open
- Lesson 32: open
- Lesson 33: open
- Lesson 34: open
- Lesson 35: open
- Lesson 36: open
- Lesson 37: open
- Lesson 38: open
Cheat sheet
-
ps aux --sort=-%membiggest memory users first -
journalctl -k --no-pagerkernel messages from the journal -
journalctl -k -p err --no-pageronly the kernel's errors -
journalctl -u app --no-pagerwhat the journal says about one service
The most important command lines of this module on one A4 page, black and white, to print or keep on your phone.
- Download PDF (PDF, 17 KB)
- View as image (PNG, 44 KB)