跳到主要內容

Linux OOM Killer 不只看 RAM 用量:從 oom_score_adj 到 cgroup 邊界的排查路徑 / Linux OOM Killer: Beyond RAM Usage

現象:服務明明還有一些可用記憶體,Linux 卻突然結束程序,甚至讓容器內的 AI 推論中斷。這不一定是整台機器的 RAM 用光;配置範圍、swap、memcg 限制與保護分數都可能讓核心進入 OOM 路徑。

Phenomenon: Linux may terminate a process even when the machine appears to have free memory. The trigger can be a cgroup, NUMA policy, swap, or allocation scope—not only total RAM.

Linux OOM decision pathallocation cannot reclaim enoughscope: system / memcg / NUMAscore memory + oom_score_adjkill selected task; continue

圖 1|Linux OOM killer 的簡化決策路徑:先確認耗盡範圍,再以記憶體用量與 oom_score_adj 評分。

Figure 1 | Simplified Linux OOM-killer path: identify the exhausted scope, then score candidates using memory usage and oom_score_adj.

機制:OOM 先看耗盡範圍,再挑候選者 / Mechanism: scope first, victim second

核心在無法透過 reclaim 或 compaction 滿足配置時,才會進入 OOM killer。mm/oom_kill.c 的 oom_badness() 依候選程序記憶體用量與 oom_score_adj 計算分數;-1000 代表強力保護。允許使用的記憶體集合取決於當次 OOM 上下文,因此 memcg OOM 不等同於整機 OOM。

The kernel enters the OOM path only after reclaim and compaction cannot satisfy the allocation. In mm/oom_kill.c, oom_badness() scores candidates using memory footprint and oom_score_adj; -1000 strongly protects a task. The allowed memory set depends on context, so a memcg OOM is not necessarily machine-wide.

影響:被殺的不一定是最吃 RAM 的程序 / Impact: the largest process is not always the victim

只看 free 會漏掉容器的 memory.max、匿名頁與 swap。端側 LLM 或影像推論的一次大配置,可能在 reclaim 來不及完成時觸發 OOM;同一 memcg 中分數較高的輔助程序也可能先被殺。

Checking only free misses a container's memory.max, anonymous pages, and swap. A large edge-LLM or image-inference allocation can hit OOM before reclaim catches up, and a higher-scored helper in the same memcg may be killed first.

可操作步驟:先留下證據,再調整邊界 / Actionable steps: capture evidence first

# Kernel OOM messages and selected victim
journalctl -k -b | grep -E 'oom|Out of memory|Killed process'

# Per-process score and adjustment
cat /proc/$PID/oom_score
cat /proc/$PID/oom_score_adj

# cgroup v2 boundary and usage
cat /sys/fs/cgroup/$CGROUP/memory.current
cat /sys/fs/cgroup/$CGROUP/memory.max

# pressure and swap
cat /proc/pressure/memory
free -h

先把 OOM 日誌、oom_score_adj、cgroup 用量與 PSI 放在同一時間線,再決定降低 batch、提高上限、增加 swap,或重新設計模型載入。不要把所有關鍵程序設成 -1000,否則可能把程序級故障換成整機失穩。

Correlate the OOM log, process adjustment, cgroup usage, and PSI before changing batch size, limits, swap, or model loading. Protecting every critical task with -1000 can trade a process failure for system-wide instability.

查核與限制:本文依 Linux 6.18.55 本機 mm/oom_kill.c(2026-11-13 查核)及官方文件整理。指令只觀測狀態,請在測試環境驗證變更。

Verification and limits: Checked against local Linux 6.18.55 mm/oom_kill.c on 2026-11-13 and official kernel documentation. Commands observe state; validate changes in a test environment.

Linux kernel VM sysctl documentation · Linux memory-management concepts · /proc documentation

留言

這個網誌中的熱門文章

text-to-cad: 讓 AI 驅動的參數化 CAD 建模成為現實 / text-to-cad: Bringing AI-Driven Parametric CAD Modeling to Reality

一個讓 AI 代理能生成、預覽並迭代 3D CAD 模型的開源框架 / An open-source harness for AI agents to generate, preview, and iterate 3D CAD models. 🔎 工具速覽 / AT A GLANCE Category AI-Powered CAD Tooling / AI 驅動 CAD 工具 Pricing Free (Open Source) / 免費(開源) BestFor Robotics engineers and product designers using AI coding agents / 使用 AI 程式碼代理的機器人工程師與產品設計師 GitHub Stars ⭐ 93 🚀 引言 / Introduction text-to-cad 是一個強大的開源框架,旨在填補大型語言模型(LLM)與專業 3D 建模之間的鴻溝。它允許開發者利用 Claude Code 或 Codex 等 AI 代理,透過程式碼生成精確的參數化幾何模型,並提供即時預覽與迭代能力。 text-to-cad is a powerful open-source harness designed to bridge the gap between Large Language Models (LLMs) and professional 3D modeling. It enables developers to leverage AI agents like Claude Code or Codex to generate precise parametric geometry via code, providing real-time preview and iteration capabilities. 🛠️ 核心功能 / Key Features 與 Codex 或 Claude Code 等代理...

kimiflare:將 Moonshot Kimi-K2.6 直接部署於 Cloudflare 的終端 AI 編碼助手 | kimiflare: A Terminal Coding Agent Powered by Moonshot Kimi-K2.6 on Cloudflare

無需中繼,在你的 Cloudflare 帳戶中直接運行強大的 Kimi 終端編碼智能體。 | No middleman, run the powerful Kimi terminal coding agent directly on your own Cloudflare account. 🔎 工具速覽 / AT A GLANCE Category AI Coding Agent / Developer Tool Pricing Free (Open-weight model / Cloudflare Workers AI pricing) BestFor Developers seeking a private, high-context terminal AI assistant with direct infrastructure control. GitHub Stars ⭐ 36 🚀 引言 / Introduction kimiflare 是一款為開發者量身打造的終端編碼智能體,它將 Moonshot 的 1 兆參數開源模型 Kimi-K2.6 與 Cloudflare Workers AI 完美結合。不同於傳統 AI 工具,kimiflare 讓流量直接在你的 Cloudflare 帳戶中流動,徹底擺脫第三方代理或 AI 閘道的束縛,實現最高等級的控制力與隱私。 | kimiflare is a terminal coding agent tailored for developers, seamlessly combining Moonshot's 1T-parameter open-source model Kimi-K2.6 with Cloudflare Workers AI. Unlike traditional AI tools, kimiflare routes traffic directly through your Cloudflare account, eliminating the n...

Orca Skills: 開源賦能,加速雲安全工作流

加速雲安全工作流 - Orca Skills 整合多種工具與插件,打造高效安全環境 | Accelerate Cloud Security Workflows with Orca Skills, a Suite of Tools and Plugins 🔎 工具速覽 / AT A GLANCE Category 开源安全工具 Pricing 开源免费 BestFor 雲安全团队、DevSecOps工程师 GitHub Stars ⭐ 33 🚀 引言 / Introduction 在雲端環境中,確保安全不僅是一種技術挑戰,更是對工作效率的一場考驗。Orca Skills 是一款專為加速 Orca 云平台安全工作流而設計的开源插件工具包。透過整合多種工具和插件,幫助我們在複雜的安全環境下保持高效率。 🛠️ 核心功能 / Key Features Orca Skills 提供了一系列功能強大的安全工作流工具,包括警報處理、影響分析、配置源頭追溯等。這些工具不僅能夠幫助我們快速定位問題,還能確保在解決問題時避免造成不必要的後續問題。 💡 技術亮點 / Tech Highlights Orca Skills 真正的亮點在于其開放性與兼容性。它不僅支持Anthropic Claude Code、Cursor 和 Codex,還能夠與 MCP(Orca Security)集成,為安全團隊提供全面的支持。 📦 快速上手 / Quick Start 要快速開始使用 Orca Skills,首先需要安裝所需環境和工具。接下來,配置 MCP 以實現與 Orca Cloud Platform 的集成。最後,測試各個技能插件以確保其正常運行。 中英文對照: 要快速開始使用 Orca Skills 首先需要安裝所需環境和工具 接著配置 MCP 以實現與 Orca 云平台的集成 最...