Debian Apache2 内存泄漏的定位与修复
一、快速缓解措施
<IfModule mpm_event_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 150
MaxConnectionsPerChild 100 # 非0,周期性回收子进程
</IfModule>
<IfModule mpm_prefork_module>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxRequestWorkers 150
MaxConnectionsPerChild 100 # 非0,周期性回收子进程
</IfModule>
sudo systemctl restart apache2。注意:将回收阈值设得过小会增加进程重建开销,需在稳定性与内存之间权衡。二、定位根因
top/htop/apachetop 观察 RES 常驻内存是否随时间单调增长;对比多个 worker/进程的内存差异,定位异常进程或请求类型。三、配置优化与容量计算
四、推荐配置示例
<IfModule mpm_event_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 150
MaxConnectionsPerChild 100 # 泄漏期间建议 50–200,稳定后再逐步调大
</IfModule>
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
修改后执行:sudo systemctl restart apache2,并持续用 top/htop/apachetop 观察 RSS 曲线是否趋于平稳。