Reducing Memory Usage
Posted by Karl Zimmerman, Last modified by Kevin Stange on 06 May 2013 02:16 PM
If your server or VM is frequently running out of memory this article should be of great assistance to you and will guide you through diagnosing what the issue is as well as listing some possible fixes.
First of all, to see how much memory you are currently using, run free -m. It will give you output such as:
total used free shared buffers cached Mem: 363 354 9 0 46 137 -/+ buffers/cache: 170 193 Swap: 1023 53 970
The "used" value (354) will almost always be close to the total memory (363) in the system. This is because Linux uses spare memory to cache data in order to reduce the reliance on the hard drive. Here 137MB are being used for cache. You can read more about this in the "Why is so much memory in use on my server when nothing is running?" article.
The main thing you're going to want to look at is the "-/+ buffers/cache:" used value (170). That is the actual amount of memory your applications are currently using. For best performance, this number should be less than your total (363) memory and in order to prevent out of memory errors, it needs to be less than the total memory (363) and swap space (1023).
In order to see where all your memory is going, just run ps aux. That will show the percentage of memory each process is using (in the %MEM column) and you can use it to identify the top memory users (usually Apache or MySQL). Remember to add together all instances of the service to calculate how much memory the service is using as a whole.
Resolving: High Apache Memory Usage
Apache is often one of the biggest memory users. Apache is run as a number of 'servers' and incoming requests are shared among them. The memory used by each server grows, especially when the web page being returned by that server includes PHP or Perl that needs to load in new libraries, and it is no uncommon for each server process to use as much as 10% of a server's memory.
In order to reduce the memory usage you can reduce the number of servers by editing your httpd.conf file. There are three settings you are going to want to look at: StartServers, MinSpareServers, and MaxSpareServers. Each can be reduced to a value of 1 or 2 and your server should still respond promptly. After changing the settings in the httpd.conf file be sure to restart Apache by running "service httpd restart" to assure the new settings are in effect.
Resolving: High MySQL Memory Usage
MySQL is relatively memory efficient on install as most memory intensive features are not enabled by default, but you can add the following lines to the /etc/my.cnf file, under the mysqld section, to free up some additional memory:
Again, make sure you restart the service for those settings to take affect. This can be done by running "service mysql restart"
Resolving: High SpamAssassin Memory Usage
SpamAssassin can also be a major memory user as it can create multiple threads/processes and each of those threads can use a good amount of memory, but SpamAssassin normally works very well with just one thread. You can reduce the 'children' setting and reclaim some memory on your server for other apps to run with.
To do this change the SPAMDOPTIONS line in the /etc/init.d/spamassassin file to:
Final Solution: Add Memory
A simple solution to resolving most out of memory problems is to add more memory. If you'd like to increase the memory on your VM, just modify your VM directly from your control panel at https://vm.steadfast.net/ and if you have a dedicated server simply email us at firstname.lastname@example.org in order to get an upgrade.