在 Ubuntu 中,“清理内存”通常涉及 释放缓存、清理内存占用、释放 Swap、删除无用文件 等。下面按常用场景给你一套完整、安全的操作方法。
Linux 会把空闲内存用作 缓存(buff/cache),这是正常的,不是内存泄漏。
free -h
sudo sync
sudo sysctl -w vm.drop_caches=3
✅ 说明:
1:只清 pagecache2:清 dentries + inodes3:全部清理(推荐)⚠️ 不要频繁执行,只适合临时释放内存。
swapon --show
free -h
sudo swapoff -a
sudo swapon -a
✅ 适用于:
⚠️ 如果物理内存不足,关闭 Swap 可能导致卡顿或 OOM。
top
按 M 按内存排序
sudo apt install htop
htop
ps aux --sort=-%mem | head
sudo systemctl restart servicename
kill PID
# 或强制
kill -9 PID
sudo apt clean
sudo apt autoclean
sudo apt autoremove
sudo journalctl --vacuum-time=7d
sudo snap list
sudo snap remove xxx
docker system prune -a
docker stats
sudo sh -c 'echo 1 > /proc/sys/vm/drop_caches'
⚠️ 脚本化、定时执行要谨慎。
✅ 以下情况 不用管:
free -h 显示 buff/cache 高❌ 内存并不是“越空越好”。
free -h
top / htop
sudo swapoff -a && sudo swapon -a
sudo apt clean && sudo apt autoremove
如果你愿意,可以告诉我:
我可以给你 针对性的清理方案。