Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Regularly recapture memory used by Apache System
I recognized that my Apache 1.3 consumes a lot of memory during different tasks (eg. running phpDocumentor multiple times allocates about 80 MB of RAM for every instance). You can gracefully restart your web server to free the allocated memory, let's say every five minutes. Graceful means that no process is killed while it is running.

I put this into a cronjob (for root) with crontab -e:
0-59/5 * * * * /usr/sbin/apachectl graceful
Note: This is probably only suitable for development web servers, since it will disrupt access to the server during the graceful restart...

[robg adds: I don't know enough about OS X's memory management to determine whether this action is really necessary or not. But I do know that the httpd processes can take a fair chunk of real memory, especially as you ask them to do more complicated things. Hence, here's a way to recapture that RAM ... I'll leave it to the comments to discuss the merits of doing so!]
    •    
  • Currently 2.33 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (3 votes cast)
 
[7,120 views]  

Regularly recapture memory used by Apache | 8 comments | Create New Account
Click here to return to the 'Regularly recapture memory used by Apache' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Regularly recapture memory used by Apache
Authored by: rootpoot on Nov 21, '05 08:28:53AM
Try lighttpd if you find Apache to be resource intensive.

[ Reply to This | # ]
Regularly recapture memory used by Apache
Authored by: melo on Nov 21, '05 09:13:50AM

Yes, a gracefull restart will release your memory, but all your apache processes are killed.

They are killed "gracefully", though, and that means that any pending requests are completed before killing the process.



[ Reply to This | # ]
Regularly recapture memory used by Apache
Authored by: nvdingo on Nov 21, '05 09:16:44AM

Not that disruptive.

the graceful option allows a running httpd process to finish what it is doing without interrupting the client that is using it, once the processes are done, they respawn, releasing memory and reloading the configuration file.

I have never been able to notice a graceful restart on my development server when i do it.
I actively hit my site while i do it each time, to make sure it reloaded correctly, and that it doesn't hang, because sometimes, if you made changes to the config, it will hang and not give you an error on the command line.

that is the only time i ever notice an interruption during graceful restart, when it fails to restart



[ Reply to This | # ]
Regularly recapture memory used by Apache
Authored by: nvdingo on Nov 21, '05 09:25:51AM

however, looking at the crontab entry there, it may be a bit excessive to gracerfully restart apache that often.

How about you schedule your run of your memory intensive operations (if possible) and schedule your gracful restart to happen after that.



[ Reply to This | # ]
httpd.conf (prefork)
Authored by: jonas_jonas on Nov 21, '05 10:17:37AM

if you need the apache webserver only für local development, you don't need all these apache processes.

running apache on unix, apache creates a few processes to serve the content. usually one as root and a few (about ten) as nobody or http or something like that.

you may change these values in the httpd.conf (for apache 1.3): http://httpd.apache.org/docs/1.3/mod/core.html#maxclients

these are a few line of my httpd.conf for apache2. i have running apache two times. one for php4 and one for php5, so i don't want to have to much useless processes:


# prefork MPM
# StartServers: number of server processes to start
# MinSpareServers: minimum number of server processes which are kept spare
# MaxSpareServers: maximum number of server processes which are kept spare
# MaxClients: maximum number of server processes allowed to start
# MaxRequestsPerChild: maximum number of requests a server process serves
<IfModule prefork.c>
StartServers         1
MinSpareServers      1
MaxSpareServers      3
MaxClients          50
MaxRequestsPerChild  0
</IfModule>


[ Reply to This | # ]
httpd.conf (prefork)
Authored by: CrazyJack on Nov 22, '05 01:23:20PM

If you're worried about memory leaks, then simply set the "MaxRequestsPerChild" to a low value. 1000 or 10000 or so. After this many requests the process will always terminate and a new one is started.



[ Reply to This | # ]
Don't waste your time with this
Authored by: plambert on Nov 21, '05 11:11:03AM

This is entirely unnecessary. Mac OS X's virtual memory management is not this stupid, and doesn't need your help.

When a process requests memory, it is allocated to the process in its virtual memory space.

When the process actually uses the memory it has allocated, it gets real memory to do so.

When the process is done using the memory and deallocates it, the OS marks that real memory as "inactive", but doesn't remove it from the process's virtual memory space. This saves a little bit of time if the process later requests additional memory.

When another processes needs to use real memory, it is allocated from free memory first, then from "inactive" memory. Neither requires anything to be paged to disk, etc.

By restarting apache repeatedly, you are ensuring that the memory must be allocated "fresh" every time. This isn't a lot of effort, but it's entirely unnecessary! That was already done the first time the process requested it. Once the process is done with it, it's entirely available to other applications, no matter what you think the numbers mean, that you see in Activity Monitor.app or /usr/bin/top.

It's a really silly idea to think that it's worth stopping and restarting processes every five minutes, given the hundreds of man years of development that have gone into the kernel's memory management techniques. The kernel knows a _lot_ more about how to manage memory than we individuals do. Just like you wouldn't rebuild the engine in your car with entirely new headers, valves, and cylinders unless you really believed you knew more about tuning engines for your situation than the manufacturer did, you probably shouldn't worry about the low levels of memory management on your OS until you truly understand what it's already doing for you.

Trust the kernel and the kernel developers. They've been solving these problems for a very, very long time.

[ Reply to This | # ]

Regularly recapture memory used by Apache
Authored by: vykor on Nov 21, '05 09:12:05PM

There are only a few good reasons to overrule the judgment of the OS regarding memory management. One would be if Apache httpd or its PHP module or such were badly coded and leaked memory. I've seen several daemons with memory leaks that grow ridiculously large (I've also seen some resident daemons that, as a hack, surreptitiously reboot themselves on timers so as to release their leaked memory).

Unless this is occurring, I don't think manual rebooting of the processes are necessary. In fact, it'd probably incur additional costs in memory accesses and cache misses by doing so, without any real benefit, as the OS would reallocate freed memory or page to disk as needed without user intervention.



[ Reply to This | # ]