在运维GitLab时,内存占用过高是常见问题。最近,一台运行GitLab的CentOS7服务器内存使用率飙升至95%以上,导致系统响应缓慢。本文将详细记录整个排查过程、问题分析、解决方案及优化建议,帮助遇到类似问题的开发者快速定位和解决问题。
free -h 显示内存使用13GB/15GB(95%+)Swap: 0B)top 和 ps aux 显示多个Puma进程占用大量内存free -h输出:
total used free shared buff/cache available
Mem: 15G 13G 263M 154M 991M 761M
Swap: 0B 0B 0Bps aux --sort=-%mem | head -20关键输出:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
git 25624 0.4 22.2 5604100 3534936 ? Sl Feb28 550:42 puma: cluster worker 3
git 18003 0.4 20.9 5427464 3337344 ? Sl Mar22 468:42 puma: cluster worker 2
git 2165 0.2 19.7 5096760 3138504 ? Sl 2024 810:45 puma: cluster worker 0
git 21320 0.3 19.1 4920068 3052844 ? Sl 2024 751:22 puma: cluster worker 1sudo gitlab-ctl restart pumasudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab编辑 /etc/gitlab/gitlab.rb:
# 减少Worker数量
puma['worker_processes'] = 2
# 限制单个Worker内存
puma['worker_memory_limit_min'] = "1024MB"
puma['worker_memory_limit_max'] = "2048MB"
# 启用内存杀手(自动重启泄漏的Worker)
puma['worker_memory_killer'] = {
'max_requests' => 5000,
'max_ram' => "2048MB",
'check_interval' => 60
}应用配置:
sudo gitlab-ctl reconfigure
sudo gitlab-ctl restartsidekiq['max_concurrency'] = 15 # 默认25,减少并发数
sidekiq['min_concurrency'] = 5安装 htop 实时监控:
sudo yum install htop设置每日自动重启Puma(防止长期运行泄漏):
crontab -e添加:
0 3 * * * /usr/bin/gitlab-ctl restart puma优化措施 | 内存变化 | 风险 |
|---|---|---|
重启Puma | 13GB → 1GB | 服务短暂中断 |
减少Puma Workers | 12GB → 6GB | 可能影响高并发 |
内存限制 | 避免泄漏 | 需监控OOM |
free -h + ps aux --sort=-%mem命令 | 用途 |
|---|---|
sudo gitlab-ctl tail | 查看GitLab日志 |
sudo gitlab-rake gitlab:memory | 检查内存使用 |
htop | 交互式进程监控 |
通过本次实战,我们不仅解决了内存问题,还建立了长期稳定的GitLab运行策略。如果你遇到类似问题,欢迎参考本文操作! 🚀