terminal.cab

Linux Basics Lesson 26 of 38

guestLevel 1Badges and progress0/100 XP, XP to the next levelStreak0

Who eats the memory

10 minutes

Learning goals

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:

  • --sort=-%mem biggest share of RAM first
  • --sort=-rss biggest occupied memory first, the same order

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 --sort

in the glossary: ps

ps 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:

  • -k only the kernel
  • -p err only errors and worse
  • -u app only one service

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:0

in the glossary: journalctl

That 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.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

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.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

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.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

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

dmesg

Before the command, in your home folder ~

  • ~/.bashrc
  • ~/.profile
  • ~/.secret
  • ~/Documents/
  • ~/logs/
  • ~/notes.txt
  • ~/projects/
  • ~/archive/
What does dmesg print?

Check works once you have chosen an answer.

Put in order

A service disappeared during the night. Put the steps in order.

Steps

  1. Read the kernel log and look for the line Killed process.
  2. Write the finding into the ticket, with the kernel line.
  3. Look at the situation now with free and ps sorted by memory.
  4. Address the cause, with a memory limit or a fix in the app.
  5. 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

  1. Question 1 of 3A process has 12 GB under VSZ and 200 MB under RSS. How much RAM does it occupy?

    Check works once you have chosen an answer.

  2. Question 2 of 3Why does the app's own log just stop after a kill by the OOM killer?

    Check works once you have chosen an answer.

  3. Question 3 of 3dmesg says Operation not permitted as lerner. What is your next step on this machine?

    Check works once you have chosen an answer.

Cards

  1. Card 1 of 3

    Tap the card or press Space to turn it over.

  2. Card 2 of 3

    Tap the card or press Space to turn it over.

  3. 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

Next: lesson 27, 03:12, was it the OOM killer? (10 min)

What you can do now: ps aux --sort=-%mem, journalctl -k -p err --no-pager

+0 XP in this lesson

  1. Lesson 1: open
  2. Lesson 2: open
  3. Lesson 3: open
  4. Lesson 4: open
  5. Lesson 5: open
  6. Lesson 6: open
  7. Lesson 7: open
  8. Lesson 8: open
  9. Lesson 9: open
  10. Lesson 10: open
  11. Lesson 11: open
  12. Lesson 12: open
  13. Lesson 13: open
  14. Lesson 14: open
  15. Lesson 15: open
  16. Lesson 16: open
  17. Lesson 17: open
  18. Lesson 18: open
  19. Lesson 19: open
  20. Lesson 20: open
  21. Lesson 21: open
  22. Lesson 22: open
  23. Lesson 23: open
  24. Lesson 24: open
  25. Lesson 25: open
  26. Lesson 26: done
  27. Lesson 27: open
  28. Lesson 28: open
  29. Lesson 29: open
  30. Lesson 30: open
  31. Lesson 31: open
  32. Lesson 32: open
  33. Lesson 33: open
  34. Lesson 34: open
  35. Lesson 35: open
  36. Lesson 36: open
  37. Lesson 37: open
  38. Lesson 38: open
1 of 38

Next: lesson 27, 03:12, was it the OOM killer? (10 min) That's enough for today

Path complete

You finished all 38 lessons of Linux Basics.

Back to the path

Cheat sheet

The most important command lines of this module on one A4 page, black and white, to print or keep on your phone.