php Memory usage

This is more of a question for an expert .. I just need a little guidance

All my php processes seem to be taking up lots of ram. Could this be causing my server to grind to a halt at high load times?

I've had a look around and most articles on the subject mention that their processes run at around 20mb each. Whereas mine are over 200mb!

eg:

208248 kB /usr/bin/php-cgi

As you can see, I really need someone to help me fine tune my server. I would be more than happy to pay for it too.

Cheers, Ryan

Status: 
Closed (fixed)

Comments

Joe's picture
Submitted by Joe on Tue, 06/30/2009 - 21:33 Pro Licensee

20MB seems pretty much impossible for PHP5, even on a 32 bit architecture system (64 bit binaries are 50-100% larger than 32 bit binaries, and dynamic languages like PHP are hit dramatically harder by the 64 bit penalty than lower level static languages, so if 20MB PHP is possible on 32 bit machines, then 30-40MB would be the low end for PHP on 64 bit machines...and since it's dynamic, it's probably closer to 40 than 30). But, it is worth mentioning a few things...

PHP memory is heavily shared because a very large portion of PHP is just wrapping up existing system libraries. So, every PHP process uses a lot of the same shared libraries for functionality. Shared libraries are practically free beyond the first instance of php-cgi. So, while you're seeing 208MB, the unique memory usage for that process is very likely more like 70MB (that's about what we're seeing on our system, though total php-cgi process size is 219MB on our 64bit system; though the line between shared and not shared is very hard to determine, and tools like top never give the whole picture).

PHP memory usage can be modified by modifying what features you load into your PHP. The fewer features you load, the smaller it will be. The default php.ini on most modern Linux distros has everything and the kitchen sink turned on, by default. I have no idea how to guide you on which features you can safely disable. On RHEL/CentOS, you'll find that a number of modules are loaded automatically from /etc/php.d. Possibly there are many you don't need. I dunno. I've never experimented much.

Thanks for clarifying that Joe.

I read this article: http://2bits.com/articles/apache-fcgid-acceptable-performance-and-better...

"Also, make sure that you do not set PHP_FCGI_CHILDREN at all. What happens is that fcgid uses it as a multiplier, not an upper maximum, and PHP will kept on breeding like rabbits, and the server will thrash and swap very quickly. "

I notice that this is mostly turned off by default now?

Joe's picture
Submitted by Joe on Wed, 07/01/2009 - 00:43 Pro Licensee

Yes, we were misled a couple of years ago about what that option meant, but it's long been unset in Virtualmin by default. ;-)

Ok .. well my server seems to be sitting at a load average of 1+ most of the time. Could be something to do with MySQL .. are the defaults good to run with for that?

Also is it possible / worthwhile to limit the number of php-cgi processes per server?

Howdy -- a few comments --

The MySQL defaults tend to be pretty decent, for servers that aren't getting clobbered. Which are most servers. I suspect that's not your issue, but we can come back to that :-)

You asked if it's possible to limit the php-cgi programs per-server. There can only be one php-cgi running per Apache process, so you wouldn't ever have more than what MaxClients is set to in Apache.

What Joe is suggesting may be a good start -- you might want to tell PHP to stop loading every possible module.

That might help -- but if your system load is high, the thing to figure out is how many requests are you serving, and what are people requesting?

If your app is system intensive, you might need to streamline it a bit. Some apps support caching, for example.

Also, how much RAM do you have? What does the "free -m" command show?

Joe's picture
Submitted by Joe on Wed, 07/01/2009 - 15:42 Pro Licensee

Look at 'top' to see which processes are most demanding. Then we can figure out what steps to take to reduce load. If you have a lot of memory (4GB+), you'd want to switch to the my-huge.cnf configuration for MySQL. It increases the size of various caches in MySQL, and can reduce disk usage at the expense of having a bigger MySQL process. On the other hand, if you have little memory, we'd want to look for more ways to reduce memory usage--swapping in/out is a very demanding task, and so pretty much anything to reduce memory usage would be a win.

I know of two sites running on my server that are MySQL intensive. One Symfony site and the other is a jam packed drupal site. Both have some sort of caching turned on.

My free -m output is:

         total       used       free     shared    buffers     cached

Mem: 3010 2710 299 0 147 1252 -/+ buffers/cache: 1310 1699 Swap: 6079 0 6079

I have 3gb physical.

Do you have an example of a my-huge.cnf that I could try?

Lastly I think one of the main problems is mail and spam being pumped through my server. I would like to turn off at least clam if that's possible?

Thanks,

Ryan

disregard my request for my-huge.cnf .. I've found it. soz.