清理 CentOS 内存占用可以从系统层面和进程层面两个方向入手。下面按常用实际操作给你一套完整方案。
free -h
或
top
重点关注:
used:已用内存buff/cache:缓存(可以释放,但不一定需要)available:系统真正可用内存⚠️ Linux 会把空闲内存用来做缓存,这是正常优化,不是内存泄漏
sync
echo 3 > /proc/sys/vm/drop_caches
解释:
1:只清理 pagecache2:清理 dentries + inodes3:全部清理(最常用)✅ 不会杀进程,生产环境可用
ps aux --sort=-%mem | head -20
或
top
# 然后按 Shift + M
常见内存大户:
例如:
systemctl restart mysql
systemctl restart docker
systemctl restart php-fpm
而不是重启系统。
systemctl list-units --type=service --state=running
关闭不必要服务:
systemctl stop 服务名
systemctl disable 服务名
docker system prune -a
查看容器内存:
docker stats
free -h
swapon -s
关闭 swap(谨慎):
swapoff -a
永久关闭(不推荐服务器):
vim /etc/fstab
# 注释 swap 行
slabtop
如果 dentry / inode_cache 异常大,通常是文件缓存问题。
reboot
yum install htop
htop
dnf install htop
✅ 不需要清理的情况
buff/cache 很大available 还很多❌ 需要清理的情况
available 很少你可以直接把下面命令的输出发给我:
free -h
top -b -n 1 | head -20
我可以帮你判断:
如果你愿意,也可以告诉我: