CentOS 清理缓存与性能优化实操
一 先判断是否需要清理
free -h、vmstat、iostat -x 1,关注 available、buff/cache、si/so(换入/换出)等指标,确认是“缓存占用高”还是“内存紧张/频繁换页”。df -h,定位大目录:du -h --max-depth=1 /var | sort -hr | head。二 清理文件系统页缓存与 Slab(仅在必要时执行)
syncecho 1 > /proc/sys/vm/drop_cachesecho 2 > /proc/sys/vm/drop_cachesecho 3 > /proc/sys/vm/drop_caches三 清理磁盘与包管理缓存
yum clean all(清理 metadata/packages/插件缓存 等,默认目录 /var/cache/yum)yum makecache 或 yum repolist(确保后续操作稳定高效)find /var/log -type f -name "*.log" -size +50M -mtime +7 -exec truncate -s 0 {} \;find /var/log -type f -name "*.log" -mtime +30 -deletejournalctl --vacuum-time=2weeks 或 --vacuum-size=100Mpackage-cleanup --oldkernels --count=2package-cleanup --quiet --leaves --exclude-bin/tmp 与 /var/tmp 可按需清理(避免删除正在使用的会话/锁文件)。四 通过内核与挂载选项优化缓存行为
sysctl -w vm.swappiness=10(默认通常为 60;数值越小越倾向保留页面在内存,仅在内存紧张时换页)。sysctl -w vm.vfs_cache_pressure=50(默认 100;数值越大越积极回收目录项/索引节点缓存,数值过小可能导致 dentry/inode 占用偏高)。sysctl -p;变更 fstab 后执行 mount -o remount / 或 systemctl daemon-reexec 再验证。五 安全与维护建议
free -h、vmstat、iostat 的趋势。yum makecache 重建元数据缓存,确保软件安装/更新正常。