Author |
|
dbnex14
Joined: 22 Jul 2015 Posts: 17 Location: Canada, Vancouver
|
Posted: Thu 20 Aug '15 6:12 Post subject: Apache 2.2 non-php memory leak |
|
|
Hi Experts,
I am using Apache 2.2, mod_winnt, no php involved, no website. This is on Windows machine.
I am using a custom .so module acting as a server for number of devices (barcode scanners). Devices send request to this server, and this server talks to database.
Each device can send 4-8 requests to do something on database (insert, update, delete, query). There could be any number of device but already at around 10 devices, I start seeing constant memory increase and within 5-10 min, 2GB cap is reached. FastMM reports no memory leak.
I wrote a PowerShell script to stop httpd process and stop and start Apache service when memo reaches 1GB but that causes lots of restarts resulting in devices loosing connection.
Much appreciated, |
|
Back to top |
|
James Blond Moderator
Joined: 19 Jan 2006 Posts: 7373 Location: Germany, Next to Hamburg
|
Posted: Thu 20 Aug '15 11:37 Post subject: |
|
|
If you don't start and stop the windows service but restart apache by httpd -k restart it does a graceful restart on windows. See http://httpd.apache.org/docs/2.2/en/stopping.html#graceful
Of cause that is just a workaround. I guess you should check of you can change the code of your custom module and free the memory after allocating is not longer needed and or a request finished. |
|
Back to top |
|
dbnex14
Joined: 22 Jul 2015 Posts: 17 Location: Canada, Vancouver
|
Posted: Thu 20 Aug '15 17:53 Post subject: |
|
|
Hi James and thank you for the reply. To clarify, I am using FastMM to detect memory leaks. FastMM is not reporting any leaks, if I intentionally introduce one, I can see a leak being cought by FastMM. That tells me that Apache is not releasing memory to OS and this happens under certain circumstances only. If I have 1-2 devices only, then this issue is not happening. So, my guess is that it is caused by high number of requests sent to Apache.
Also, my PowerShell script is doing this to stop apache process and service and to start it (if memory has reached ca 0.8GB):
//below is PowerShell code
# If working set of httpd or httpd#1 is greather 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;
}
As you can see, I am stopping httpd process, then stopping Apache service, then starting the service which will spawn new httpd process. Are you saying this is not good way of doing it?
Also, here are the Apache settings I am using:
#Commented out these 3
#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>
I agree with you that recycling process should be temporary solution but right now, the problem is that I can not detect any leaks although Task Manager shows constant growth in memory. Anything you could suggest based on above?
btw, I dont know how to issue httpd -k from PowerShell
Much Appreciated, |
|
Back to top |
|
James Blond Moderator
Joined: 19 Jan 2006 Posts: 7373 Location: Germany, Next to Hamburg
|
Posted: Thu 20 Aug '15 22:10 Post subject: |
|
|
it is just the path to the binary.
Instead of
stop-process -name $MyHTTPD -force
and
stop-Service $ServiceName;
and
start-Service $ServiceName;
you do a one liner
Code: | C:\path\to\apache\bin\httpd.exe -k restart |
|
|
Back to top |
|
James Blond Moderator
Joined: 19 Jan 2006 Posts: 7373 Location: Germany, Next to Hamburg
|
Posted: Thu 20 Aug '15 22:13 Post subject: |
|
|
It doesn't necessarily that the memory leak go to the OS, but pumping up the httpd process.The winnt mpm has a number of threads. |
|
Back to top |
|
dbnex14
Joined: 22 Jul 2015 Posts: 17 Location: Canada, Vancouver
|
Posted: Thu 20 Aug '15 22:35 Post subject: |
|
|
James Blond wrote: | It doesn't necessarily that the memory leak go to the OS, but pumping up the httpd process.The winnt mpm has a number of threads. |
Sorry, would you mind elaborating on this please.
Much appreciated |
|
Back to top |
|
dbnex14
Joined: 22 Jul 2015 Posts: 17 Location: Canada, Vancouver
|
Posted: Thu 20 Aug '15 22:51 Post subject: |
|
|
James Blond wrote: | you do a one liner
Code: | C:\path\to\apache\bin\httpd.exe -k restart |
|
Awesome, that works. Since my service was installed like this:
httpd.exe -k install -n "Apache[XYZ]" -f "C:\MY\Apache Software Foundation\Apache2.2\conf\httpd-XYZ.conf"
, I just modified slightly the command:
C:\MY\Apache2.2.29\bin\httpd.exe -k restart -n "Apache[XYZ]" -f "C:\Program Files (x86)\Apache Software Foundation\Apache2.2\conf\httpd-XYZ.conf";
and that is restarting it.
James, would you mind explaining what is the difference between these 2 ways of restarting it?
Much appreciated |
|
Back to top |
|
James Blond Moderator
Joined: 19 Jan 2006 Posts: 7373 Location: Germany, Next to Hamburg
|
|
Back to top |
|
dbnex14
Joined: 22 Jul 2015 Posts: 17 Location: Canada, Vancouver
|
Posted: Fri 21 Aug '15 20:18 Post subject: |
|
|
James Blond wrote: | It doesn't necessarily that the memory leak go to the OS, but pumping up the httpd process.The winnt mpm has a number of threads. |
Hi James,
Would you mind explaining this a bit?
I am aware that in winnt mpm, there is only one parent and one child with ThreadPerChild threads in it. I do see the child memory going up and not released back.
Another error that I was seeing in Windows Event Viewer was the "(OS 10048)Only one usage of each socket address (protocol/network address/port) is normally permitted. : make_sock: could not bind to address 0.0.0.0:81". And in Apache error.log, I was seeing "An operation was attempted on something that is not a socket)"
Both stopped happening when I added to my config this:
Win32DisableAcceptEx
However, now I see error in Event Viewer "[notice] Disabled use of AcceptEx() WinSock2 API" which is what above directive is supposed to do as per http://httpd.apache.org/docs/2.2/mod/mpm_winnt.html so not sure why Event Viewer reports that as error |
|
Back to top |
|
James Blond Moderator
Joined: 19 Jan 2006 Posts: 7373 Location: Germany, Next to Hamburg
|
Posted: Tue 25 Aug '15 16:50 Post subject: |
|
|
That last notice is ok. It is only a notice, not an error or a warning.
It is only because of the windows socket system. Apache was desinged to run under linux first and later came windows in the focus. You can get rid of that if you can upgrade to 2.4. Apache 2.2 is almost EOL.
Your module doesn't free the memory. That is for sure else the memory usage won't grow until there is no more memory left. I don't know your module, but it seems that at least the apache process doesn't leak. Since winnt mpm is using threads you can restart apache without killing the child process. It just creates new threads and kills the old ones. |
|
Back to top |
|
dbnex14
Joined: 22 Jul 2015 Posts: 17 Location: Canada, Vancouver
|
Posted: Mon 31 Aug '15 21:53 Post subject: |
|
|
And I do that by the command you posted earlier?
Code: | C:\path\to\apache\bin\httpd.exe -k restart |
|
|
Back to top |
|
James Blond Moderator
Joined: 19 Jan 2006 Posts: 7373 Location: Germany, Next to Hamburg
|
Posted: Tue 01 Sep '15 10:08 Post subject: |
|
|
Yes the -k restart does a graceful restart. On linux it is a bit different, but a restart on Windows is always graceful. |
|
Back to top |
|
dbnex14
Joined: 22 Jul 2015 Posts: 17 Location: Canada, Vancouver
|
Posted: Tue 01 Sep '15 17:19 Post subject: |
|
|
James Blond wrote: | Yes the -k restart does a graceful restart. On linux it is a bit different, but a restart on Windows is always graceful. |
I am doing that, thanks James. In regards to module not releasing memory, the strange thing is that FastMM memory manager will not detect any memory leaks.
I tried introducing memory leak intentionally just to check FastMM and it would discover it. So, I am not sure how to find the leak if none is reported. Any suggestion you can make here?
I was also trying to set MaxRequestsPerChild to 20 to recycle process after 20 connections but that of course does not remove the leak, that seem to be same as using graceful restart using "httpd -k restart"?
Much appreciated |
|
Back to top |
|
James Blond Moderator
Joined: 19 Jan 2006 Posts: 7373 Location: Germany, Next to Hamburg
|
Posted: Tue 08 Sep '15 10:42 Post subject: |
|
|
Yes the max request thing does the same. |
|
Back to top |
|
dbnex14
Joined: 22 Jul 2015 Posts: 17 Location: Canada, Vancouver
|
Posted: Mon 14 Sep '15 17:23 Post subject: |
|
|
James Blond wrote: | Yes the max request thing does the same. |
Thanks James |
|
Back to top |
|