Apache 内存泄漏 - 未使用 PHP

Apache Memory Leak - no PHP used

我的 Apache 2.2 模块 (Windows) 似乎需要内存,任务管理器显示内存不断增长,直到达到 ~2GB,然后它崩溃了。我根本没有使用 PHP,这不是一个网站,而是一个充当服务器的模块。多个条码扫描设备向该服务器发送请求,由该服务器对数据库进行插入、更新、删除、查询等操作。如果我有 10-15 台设备同时工作,我就会看到这个问题。

我正在使用 FastMM 检测我的 Apache 模块中的内存泄漏,而 FastMM 没有报告任何泄漏。如果我有意介绍一个,我可以看到 FastMM 正在咳嗽泄漏。

这告诉我 Apache 没有向 OS 释放内存,这只在某些情况下发生。如果我只有 1-2 台设备,则不会发生此问题。所以,我的猜测是它是由发送到 Apache 的大量请求引起的。

作为临时解决方案,我使用 PowerShell(版本 2.0 或 4.0,取决于机器)脚本在达到内存阈值时重新启动 Apache。我的 PowerShell 脚本这样做是为了停止 apache 进程和服务并启动它(如果内存已达到 ca 0.8GB)并且这一切都有效,我测试了它:

# If working set of httpd or httpd#1 is greater than threshold, stop and start
if($procobj.workingset -gt $Threshold)
{
# $ProcName is name of process reported by PowerShell (as httpd, httpd#1, httpd#2, ...)
echo $("Memory for " + $ProcName + " exceeds Threshold");

# Stop httpd process
stop-process -name $MyHTTPD -force

# Stop service $ServiceName (this is name of service in Windows->Services)
echo $("---> Stopping service: " + $ServiceName);
stop-Service $ServiceName;

# Start service $ServiceName (this is name of service in Windows->Services)
echo $("---> Starting service: " + $ServiceName);
start-Service $ServiceName;
}

如您所见,我正在停止 httpd 进程,然后停止 Apache 服务,然后启动将生成新 httpd 进程的服务。

此外,这是我使用的 Apache 设置:

#Commented out these 3 in httpd.conf
#KeepAlive On
#MaxKeepAliveRequests 0
#KeepAliveTimeout 3000

#these are in mod_mpm
# WinNT MPM
<IfModule mpm_winnt_module>
ThreadsPerChild 300
#MaxRequestsPerChild 0
MaxRequestsPerChild 50
#According to Apache documentation, if you get "An operation was
#attempted on something that is not a socket), you should use this to
#disable AcceptEx() WinSock v2 API. See:
# http://httpd.apache.org/docs/2.2/mod/mpm_winnt.html
Win32DisableAcceptEx
</IfModule>

我知道这应该是临时解决方案,但现在的问题是,尽管任务管理器显示内存不断增长,但我无法检测到任何泄漏。

非常感谢,

我的经历和你完全一样,我也用 Memproof 检查了我的服务器软件是否有泄漏,除了 Windows 内核外什么也没发现。我将 MaxRequestsPerChild 设置为 1000000,每个 child 有 1500 个线程。使用 Apache 2.4,我不必使用 powershell 脚本来重置它 - Apache 会自动为我执行您的脚本执行的操作。这可能是 Windows(或 Apache)处理重叠连接的方式的问题。不过,我猜,我们目前使用的解决方案可以正常工作。也许这不会发生在 Linux 服务器配置上,但我的服务器软件只能在 Windows 上运行!问候。