Linux 内存不足:诊断 OOM 并解决卡死问题

从内核日志确认 OOM 杀进程,定位内存元凶,安装 earlyoom 并调整 swappiness,让 Linux 保持流畅响应。

Linux日志监控内核内存内存dmesg 内核日志中风险
先运行 dmesg -T,确认内核的 OOM killer 是否一直在杀进程。

快速开始

Linux 内存耗尽时,系统会在 swap 中颠簸、桌面锁死,内核的 OOM killer 会悄悄杀掉进程。本指南从确认问题入手,一步步给出让系统保持响应的修复方案。

症状

?桌面卡死、应用无故消失、登录超时,回来时发现没关的程序已经不见了。

确认内核确实在因内存不足而杀进程(OOM)

在调整任何参数之前,先证明问题确实出在内存上。内核每次触发 OOM 杀进程都会记录日志,用 dmesg 查看带真实时间戳的消息,寻找 'Out of memory' 或 'Killed process' 之类的条目。看到这些记录即可确认诊断;如果没有,则需要另找原因。

dmesg 默认显示开机以来的秒数,加 -T 后转成真实日期时间,方便和故障时间点对应。

命令
$ dmesg -T

查看内存和交换分区还剩多少可用

free -h 以人类可读的单位显示已用、空闲和可用内存以及交换分区情况。重点看 'available(可用)' 一列:如果它接近零,且 swap 几乎用满,说明系统已濒临颠簸——这正是触发 OOM 杀进程前的典型状态。

使用 free -h 以 GB/MiB 等人类可读单位查看内存和 swap 的使用量、剩余量和可用量。

命令
$ free -h

用 htop 找出占用内存的元凶

启动 htop,按内存排序(F6 选择 MEM%),实时观察哪些进程在消耗内存。内存和 swap 的彩色仪表一眼可见全貌,还能定位到反复把系统推向 OOM 边缘的具体进程。

启动一个交互式进程查看器,顶部有各 CPU 核心内存交换分区的彩色仪表,支持搜索、排序和直接结束进程。

命令
$ htop

用 ps aux 生成文本快照

如果需要留档对比或粘贴到问题报告里,ps aux 一次性列出所有进程及其属主、CPU 和内存占用。对比多次快照,留意 %MEM 持续攀升的进程。

Linuxps aux:查看全部进程的完整快照低风险步骤 4可选

使用经典的 ps aux 命令列出系统所有进程,包含所属用户、CPU、内存和命令列。

命令
$ ps aux

安装 earlyoom,避免桌面彻底卡死

没有 earlyoom 时,Linux 会等到内存彻底耗尽才开始杀进程——那时机器往往已在 swap 风暴中失去响应。earlyoom 会在系统尚可响应时就提前干掉最大的内存占用者。启用该服务并确认其处于运行状态;如需回退,再次禁用即可。

安装一个极小的守护进程,在系统卡死之前干掉失控的内存大户。

命令
$ sudo systemctl enable --now earlyoom
▾ 撤销
撤销
$ sudo systemctl disable --now earlyoom

可选:调整 swappiness

vm.swappiness 控制内核把内存换出到 swap 的积极程度。较低值(如 10)会让更多数据留在内存中,适合经常颠簸的交互式桌面。先查看当前值,再在线设置新值;恢复默认值 60 即可撤销。该设置立即生效,但重启后不会保留。

sysctl 可读写运行中的内核参数;vm.swappiness 控制内核换出内存的积极程度。

命令
$ sysctl vm.swappiness && sudo sysctl -w vm.swappiness=10
▾ 撤销
撤销
$ sudo sysctl -w vm.swappiness=60

用 vmstat 验证效果

运行 vmstat 观察几个采样周期的 si/so(换入/换出)列。修复之后,swap 活动应接近零,等待 I/O 的进程(b 列)不应堆积。swap 流量稳定且偏低,说明内存压力已得到控制。

vmstat 每秒输出一行 CPU、内存、swap 换入换出和 I/O 等待数据,比 top 更直观地判断瓶颈类型。

命令
$ vmstat 2 5
!sysctl 的修改立即生效但重启后失效,调低 swappiness 可能影响吃内存的工作负载;earlyoom 可能会杀掉你正在使用的应用——测试内存压力前先保存工作。

如何确认已解决

vmstat 显示 swap 换入/换出几乎为零,正常负载下可用内存稳定保持在较高水平。

一键全部撤销

上面每一条命令都可以撤销。按此顺序执行即可全部还原。

一键全部撤销
$ sudo sysctl -w vm.swappiness=60
sudo systemctl disable --now earlyoom

执行前请先确认——部分步骤之间可能并非相互独立。

来源

  • dmesg(1) - Linux manual page
    Ubuntu Manpages · technical · 2026-09-17
    依据: The -T, --ctime option decodes timestamps into a human-readable format, and -w (--follow) waits for new kernel messages.
  • free(1) - Linux manual page
    Ubuntu Manpages · technical · 2026-09-17
    依据: free displays the total amount of free and used physical and swap memory, and -h shows all output fields automatically scaled to human-readable units.
  • htop(1) — interactive process viewer
    man7.org · technical · 2026-09-17
    依据: The htop man page describes it as an interactive text-mode process viewer that allows scrolling vertically and horizontally, unlike top.
  • htop - an interactive process viewer
    htop.dev · vendor-doc · 2026-09-17
    依据: The official htop site describes real-time CPU, memory, and swap meters plus interactive process sorting and killing.
  • Ubuntu Manpage: ps - report a snapshot of the current processes
    manpages.ubuntu.com · technical · 2026-09-17
    依据: "ps displays information about a selection of the active processes"; the u option selects the user-oriented format with USER, %CPU, %MEM, and COMMAND headers.
  • ps - report a snapshot of the current processes
    man7.org · technical · 2026-09-17
    依据: The ps page documents the a, u, and x options ("u Display user-oriented format" including USER, %CPU, %MEM, and COMMAND columns) and describes ps as reporting "a snapshot of the current processes".
  • earlyoom - Early OOM Daemon
    GitHub / rfjakob · community · 2026-09-17
    依据: README instructs installing the package and running sudo systemctl enable --now earlyoom, and documents defaults like -m 5 (kill at 5% RAM) and --prefer.
  • earlyoom README (raw)
    GitHub / rfjakob · community · 2026-09-17
    依据: Shows earlyoom.service and 'sudo systemctl enable --now earlyoom' plus tuning flags -m, --prefer and the low-memory kill logic.

最后验证: 2026-09-21

相关指南