TT Lab
Blog Get started
Learn Learning paths Courses

Linux Incident Response

Diagnosing Memory and OOM

Continue in TT Lab

Goal

You read memory metrics accurately, distinguish the memory a process has actually taken hold of from address space that was merely reserved, confirm what the OOM Killer looks at when it chooses, and then, among four processes that "look like they use a lot of memory", pick and stop only the one real cause.

Why it matters

Most misdiagnoses of memory problems start with looking at the wrong column. You should look at the available column of free, not the free column, and at RES (RSS) in top, not VIRT. The OOM Killer's score is also RSS-based - a process that merely reserved 64GB with mmap is not even a candidate. And inside a container free shows the whole host, so the real limit must be read from the cgroup files. The reason step 7 of this lab even grades "if it isn't there, write that it isn't" is that in the field too, the first action is to check whether that file exists.

In the incident in step 8, "large" is not evidence of guilt. The one with a large VIRT, the one with the largest RSS, and the one creating cache are all plausible, but what heads toward the limit is anonymous memory that grows over time. Killing the wrong one only cuts off that service's customers, and the graph keeps rising.

Steps

Work in the /root/mem directory.

  1. From /proc/meminfo, write the MemTotal value as a kB number only to /root/mem/total_kb.txt.
  2. Write the MemAvailable value, as a kB number only, to /root/mem/available_kb.txt.
  3. Start a process whose actual resident memory (VmRSS) is 100MB or more in the background and write its PID to /root/mem/hog_pid.txt. It must stay alive through all the following steps.
  4. Write the VmSize and VmRSS of that process to /root/mem/vm.txt as the following two lines.
    • vsize_kb=<값>
    • rss_kb=<값>
  5. Measure the oom_score_adj this container originally received on PID 1, which nobody has touched, and write it as initial_adj=<값> (value); then raise the oom_score_adj of the process from step 3 to the maximum, 1000, and write the oom_score read after that as oom_score=<값>, both to /root/mem/oom_score.txt.
  6. Create /root/mem/topmem.sh <N>. It prints the top N processes by RSS in the form <rss_kb> <comm> in descending order. It must be exactly N lines.
  7. Write the cgroup memory limit of this container, as it is, to /root/mem/limit.txt. If it is an environment where that file cannot be read, write unavailable.
  8. Incident incident ts-mem-pressure: the container's memory usage keeps rising. Classify the four services (tsmem-indexer, mapper, cache, backup) as one of leak, reserve-only, page-cache, steady, or shmem, write four lines <서비스>: <코드> (service: code) and one evidence: line to /root/incident/ts-mem-pressure/report.txt, and stop only the one cause that is increasing usage. Do not touch the other three or the archive files.

Notes

Read total memory

From /proc/meminfo, write the MemTotal value as a kB number only to /root/mem/total_kb.txt.

The total memory size the kernel sees is at the top of meminfo. Remember that the host's value is visible even inside a container; you compare it again in step 7. Write only the number, in kB as it is.

Distinguish MemAvailable from MemFree

Write the MemAvailable value, as a kB number only, to /root/mem/available_kb.txt.

Linux uses spare memory as page cache. There is a separate value that counts in the reclaimable amount.

Actually hold memory

Start a process whose actual resident memory (VmRSS) is 100MB or more in the background and write its PID to /root/mem/hog_pid.txt. It must stay alive through all the following steps.

If you only allocate and don't touch it, RSS doesn't grow: the kernel attaches a page only at the moment of first use. Start a program that grabs a large buffer, actually writes a value into every page, and then sleeps for a long time.

Compare VmSize and VmRSS

Write the VmSize and VmRSS of that process to /root/mem/vm.txt as the following two lines.

Both are in the status file the kernel provides for each process. One is the reserved address space, the other is the part actually loaded into RAM; compare which one is how large.

Adjust the OOM score

First, measure the oom_score_adj this container originally received on a process nobody has touched (PID 1), and write it as initial_adj=<값> (value). Then raise the oom_score_adj of the process from step 3 to the maximum, 1000 ("the first to be killed"), and write the oom_score read after that as oom_score=<값>. Write both lines to /root/mem/oom_score.txt.

Each process has a separate file for adjusting the score and one for reading it. The container's initial value is decided by whoever started it: Kubernetes gives a different value for each Pod QoS class (Guaranteed, Burstable, BestEffort). Anyone can raise it above that value, but lowering it requires the capability to exceed resource limits, so it is refused in this Pod. Be sure to read the score after changing it.

Pick the top RSS processes

Create /root/mem/topmem.sh <N>. It prints the top N processes by RSS in the form <rss_kb> <comm> in descending order. It must be exactly N lines.

Collect the resident memory value from the status files of all processes and sort numerically. Kernel threads do not have that entry, and some processes exit while you scan, so skip the entries that fail.

Find the container's real limit

Write the cgroup memory limit of this container, as it is, to /root/mem/limit.txt. If it is an environment where that file cannot be read, write unavailable.

The free command shows the host. The real ceiling is in the cgroup files. If it isn't there, write that it isn't.

Incident: memory usage keeps rising, and there are four suspects

Running incident ts-mem-pressure raises an incident in which this container's memory usage keeps rising. The four services tsmem-indexer, tsmem-mapper, tsmem-cache, and tsmem-backup each look "like they use a lot of memory". Classify each as one of leak (the memory it uses keeps growing), reserve-only (it only reserved address space), page-cache (files it read are held as cache), steady (large but not growing), or shmem (shared memory, tmpfs), write four lines <서비스>: <코드> (service: code) and evidence: <관찰한 출력 한 줄> (one line of output you observed) to /root/incident/ts-mem-pressure/report.txt, and stop only the one cause that is increasing usage. Stopping or restarting the other three, or deleting the archive files to empty the cache, is a failure.

Direction: "large" and "growing" are different things, and so are "address space", "memory actually loaded", and "cache that can simply be read again". Look not at a value taken once but at the change over time and the kind of memory. Tools: /proc//status, the cgroup's memory.current and memory.stat, the service logs (/var/log/tsmem). Concretely: put the VmSize and VmRSS of the four processes side by side, and measure VmRSS twice, about 10 seconds apart. Of the container's usage, the shares of anonymous memory and file cache are on separate lines, anon and file, in memory.stat.