Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

We've got a website on our dedicated server which seems to be causing problems.

Recently it has been crashing our dedicated server - it's very annoying. It will crash at random times. The tech support team at the hosting company says that it is because of lack of memory. They have increased apache's maxclients setting, but to no avail.

So the last thing it can be (we've moved mysql and images off the server) is that there's a php script somewhere which is using a hell of a lot of memory. It's quite a popular website, not by stack overflow standards of course, but it's relatively popular.

Possibly there is a memory leak somewhere or some sort of infinite loop created somewhere or something.

How can I pinpoint which scripts are using the most memory?

share|improve this question
up vote 1 down vote accepted

that it is because of lack of memory. They have increased apache's maxclients setting

Look for another service provider.

If you don't have enough memory to service the current number of clients, then increasing the number of clients will make the problem worse.

First thing is to check that it's logging PHP errors properly (i.e. writing them to the webserver error_log). You can test this by simply accessing a (deliberately) badly written script, e.g.


Out-of-memory errors will be written to this error log - including details of which script was executing at the time and where it fell over.

You can also add code to record details of memory usage (e.g. with an auto-prepend script) rather than wait for it to fail. It would be a good idea to implement this via a registered shutdown function.

This should give you some priorities for re-factoring.

Meanwhile, as a seperate exercise, you should set a cron job to start logging the logging the number of httpd processes, and the amount of free memory e.g.


m=`free | grep '+'`
p=`ps aux | grep httpd | wc -l`

echo $t $m $p >> /var/log/webload

Then graph the results to get an idea of how many clients you can serve with a given amount of memory (NB since good I/O performance depends on caching, then depending on what I/O is going on on your system, you probably want to aim for around 50% of memory free).

This will also provide a mechanism for measuring how effective your refactoring is.

Increasing the max memory available to each PHP process is simply a matter of updating the php.ini file.

Do apply the usual process for PHP / webserver tuning - i.e. check caching, compression working optimally, log and analyse database queries.

NB sometimes you just need more hardware.

share|improve this answer
In the end we moved the site to an amazon elastic cloud server, cached everything, enabled compression, set up CDNS for images and content and all sorts! Its been stable recently, so I would assume that maybe either there's something that I fixed in amongst that, or maybe our dedicated server is just in need of an upgrade! – Thomas Clayson Dec 9 '11 at 11:04

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.