Mysql – Apache with mod_php high memory utilization

apache-2.2haproxymagentomod-phpMySQL

We have Magento application deployed on Apache with mod_php and mysql.
I have observed that sometime apache server starts consuming high memory which causes memory swapping and results in high load on servers. whenever there is high load on apache server, the apache processes which are causing the high load were in sleep mode at mysql end and CLOSE_WAIT state at client side. Any help is appreciated to resolve this issue.

Best Answer

@raj you'll probably find that the processes are in an S state as they are waiting on another process (in a D state) bound by I/O ie. For the machine to stop swapping.

So the key is to address two key issues, standard memory allocation and memory commitment.

When running Magento, it is fairly easy to massively over commit on RAM simply by setting PHP Max memory limits too high. Under normal circumstances, it won't have any ill effects, but the moment the app tries to use that memory, it will almost immediately go into swap (if you've run out of RAM that is).

Then, you'll likely find that Apache isn't the main concern for memory usage, but rather MySQL is more likely the culprit.

You haven't mentioned your server specification or any specific details for your store, so it would be impossible to accurately advise. But simply revising your Apache and MySQL configuration would be an easy place to start. Drop the number of Apache threads, MySQL connections, allocated memory commitments. Then start graphing with a tool like Munin to actually be able to correlate traffic, threads, CPU usage and memory usage. Without doing so, you are working blind.

But I would advise to stop poking around netstat as you are a long way off needing to diagnose TCP states; you need to start from the beginning.