CH-J Toolsserver management over SSH
Menu
All guides and articles →

How to find out why Linux terminated a process: OOM Killer

Distinguish between kernel OOM, cgroup limit, systemd-oomd and plain SIGKILL. Find evidence in the log and fix the memory budget instead of endless restarts.

Killed or return code 137 does not yet determine the cause

A process may suddenly disappear without any error message. The shell will print Killed or the parent tool will show code 137. This is a useful clue to signal termination, but not in itself evidence of low memory. SIGKILL could be sent by the manager, service manager, container platform, or memory pressure response mechanism.

The Linux OOM killer is one possible cause. When memory requirements fail to be met within a given scope, the kernel can select and terminate a task. The scope can be the entire machine or a limited cgroup, such as a specific service or container. Therefore, a machine may report free RAM after a process terminates, but may actually be experiencing OOM before.

The first goal is to match the exact time, process identity, unit or container, and termination record. Only then adjust capacity or limits. Repeated restarts can recreate the same memory spike while covering up evidence of the original problem.

Find the kernel entry at the correct boot and time

Use the kernel journal or any available kernel log. Start with a limited period around the outage and do not confuse UTC with local application time. Messages may include the phrase Out of memory, oom-kill, or Killed process. It is important to read the surrounding context, as a single line may not distinguish between a global and a cgroup event.

sudo journalctl -k --since "-2 hours" --no-pager
sudo journalctl --list-boots
# Pokud událost nastala v předchozím uloženém bootu:
sudo journalctl -k -b -1 --no-pager

Dmesg can provide a current kernel buffer, but it has limited history and access may be restricted. Don't assume from empty output that there was definitely no OOM. The journal may be volatile, the event may have already been rolled back, or you are looking at an incorrect boot. Verify the bounds of the available data and formulate the certainty of the result accordingly.

Compare the PID in the old record with the time and process name. The same number may be used later by another program. In the event of an incident, keep the relevant section and service connection. Do not publicly post the entire operational log with internal paths or request data that are not needed for explanation.

Global OOM and service limit may look similar

For global pressure, you consider the overall memory budget of the machine, swap availability, and the concurrency of all services. For cgroups, only a specific branch may be limited. So, higher free memory on the host does not preclude reaching the container limit. Memory also accounts for more than just the simple sum of RSS processes, so use metrics appropriate to the limit to make your decision.

free -h
swapon --show
# Stav služby s nahrazeným skutečným názvem:
systemctl show APLIKACE.service -p Result -p ExecMainCode \
  -p ExecMainStatus -p MemoryCurrent -p MemoryPeak -p MemoryMax -p ControlGroup

Availability of individual features depends on systemd version and accounting. Today's MemoryCurrent or free is not a reconstruction of the historical maximum before termination. Use an existing time series if available. If not, provide that instead of the estimate given as a measurement.

Also check the parent slice and shared limits. Two services may look reasonable on their own, but when running together they can exhaust their shared limit. So don't increase one value without knowing the rest of the hierarchy and physical capacity. The limit is there to protect other traffic, and removing it could put the entire machine at risk instead of just one process.

Cgroup v2 provides specific event counters

On a system with cgroup v2, the memory.events file for the cgroup may be useful. Find the path from the ControlGroup or container management tool, don't guess it from the application name. Counters like oom and oom_kill help document pressure and termination within a given scope. Their interpretation must respect the group lifecycle and hierarchical accounting.

# Ilustrační cesta: nahraďte ji skutečnou cgroup služby.
cat /sys/fs/cgroup/system.slice/APLIKACE.service/memory.events
cat /sys/fs/cgroup/system.slice/APLIKACE.service/memory.current
cat /sys/fs/cgroup/system.slice/APLIKACE.service/memory.max

A non-zero counter alone does not indicate that a particular termination occurred right now. Compare the time series or change of the counter and the associated log. The memory.events.local file can differentiate local events from the hierarchical summary as needed. For a defunct and recreated cgroup, the history may not be preserved as you expect.

Additionally, the container engine may provide its own OOMKilled status and environment manager events. Verify which container run the status belongs to. The status of an older instance is not automatically an explanation for today's restarted application. Record the run identifier and associated time, not just the container variable name.

Systemd-oomd and other termination mechanisms

Systemd-oomd is a user mechanism that can react to memory pressure and intervene before the same situation that the kernel OOM killer would handle. Therefore, there may not be a corresponding kernel Killed process entry even though the process actually died due to memory policy. Check if this service is even installed on the system.

systemctl status systemd-oomd.service --no-pager
sudo journalctl -u systemd-oomd.service --since "-2 hours" --no-pager
# Další kontext konkrétní postižené služby:
sudo journalctl -u APLIKACE.service --since "-2 hours" --no-pager

Other causes include service manager timeout, administrator intervention, or a controlled termination during deployment. The SIGKILL code value itself does not distinguish between these. Also, PHP memory_limit can terminate a specific request on its own error without a kernel OOM. An application can fail to allocate memory in ways other than forcibly killing the process.

Therefore, to confirm the cause, at least combine the state of the service, its time, and the corresponding manager or kernel event. Do not label any restart as OOM just because the program is working with a large file. This could miss a faulty deployment, an operator signal, or a separate application crash.

Why was this process chosen?

Victim selection is not a simple rule of "always terminate the largest RSS". The memory context and selection rules, including the relevant score and adjustments, are decisive. OOMScoreAdjust can influence the willingness of the system selection to terminate the process. However, it does not mean that setting protection for all important processes will create new memory.

# Pro aktuální existující PID, pouze čtecí kontrola:
cat /proc/PID/oom_score
cat /proc/PID/oom_score_adj
ps -p PID -o pid,ppid,user,comm,rss,vsz,lstart

RSS and VSZ virtual address space are not the same thing. Large virtual extent does not necessarily mean the same physical footprint. Shared pages also complicate the sum of individual processes. If you want a worker budget, measure the appropriate metrics and real load instead of the sum of the largest numbers without their definition.

Protection score adjustments are part of a well-thought-out service policy. Moving the selection to another process can damage the database or administrative access without the cause going away. The fundamental question remains why the claims exceeded the actually available budget.

Remedy: capacity, concurrency, growth, or specific error

Compare normal usage and the peak at failure. The problem could be too many workers, a single large import, a memory leak, or an unbounded queue. Each scenario requires a different intervention. Reducing concurrency may maintain functionality but increase wait times; therefore, measure its impact along with service responsiveness.

Increasing the limit only makes sense with a documented margin for the entire environment. Adding swap can change behavior under pressure, but it is not a universal RAM replacement or a fix for unbounded growth. Slower I/O can worsen latency to the point where a service stops serving its purpose before anything can kill it. Also consider the specific swap policy of the cgroup.

If you suspect a leak, compare consumption over time under similar conditions and use targeted application diagnostics. A generic kill -9 command or an automatic restart every few minutes will not fix the cause. If a restart is a temporary fix, record it as such and leave it open to search for a specific error.

Verification and recording of the result

After the targeted change, repeat the corresponding safe working scenario and monitor the memory peak, the relevant counters and the availability of the application. Also verify the other services: the higher limit of one must not silently exhaust the reserve of the rest of the machine. The test period should include the cycle that originally caused the problem.

  • It is documented who terminated the process and which run it belonged to.
  • There is a distinction between global capacity, cgroup ceiling, and user memory policy.
  • The remedy corresponds to a confirmed concurrency or growth, not just code 137.
  • The new test passed without the same incident and with an acceptable response.
  • Missing historical data are listed as a limitation of the conclusion.

In CH-J Tools, a memory overview may indicate pressure, but a definitive explanation for the termination requires corresponding server events. The conclusion "the process was terminated by the kernel in this cgroup at the given time" is verifiable. A general "ran out of RAM" without time and operational context does not yet determine the correct fix.

Frequently asked questions

Is exit code 137 always OOM?

No. It often corresponds to a SIGKILL termination, which can have other causes. You need the associated log and run status.

Why is there a lot of free RAM again after OOM?

A terminated process may have freed up some memory. The current state is not a historical peak before the event.

Can a container get OOM even if the host has space?

Yes. It can reach its own or parent cgroup limit.

Will adding swap solve OOM?

Not universally. You need to consider workload, latency, concurrency, and the cause of the growth. Swap will not cure an unbounded memory leak.

Detailed documentation

Linux kernel: cgroup v2 and memory.events →

systemd: user OOM manager →

Linux: oom_score process →

systemd: memory limits and accounting →

Biometric unlock and security →

All tutorials by category →