How do I reduce memory usage?

If your server or VPS 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:        946     924     22      0      148     483
-/+ buffers/cache:  291    654
Swap:       2047     23   2023


The “used” value (924) will almost always be close to the total memory (946) in the system. This is because Linux uses spare memory to cache data in order to reduce the reliance on the hard drive. Here 483MB are being used for cache.


The main thing you’re going to want to look at is the buffers/cache row used value (291). That is the actual amount of memory your applications are currently using. For best performance, this number should be less than your total (946) memory and in order to prevent out of memory errors, it needs to be less than the total memory (946) and swap space (2047).

On our VPS accounts you are not going to have any buffers or cache listed, as that is done outside of your VPS for greater overall efficiency. You can simply use the used and free columns as they are. Do note though that the amount of memory available may be higher than what is guaranteed on your account and to maintain performance and stability you will want to keep your memory usage under that guaranteed amount. VPS accounts will also not have any swap allocated to them, as that is also managed outside of the VPS, so as soon as you go over your available amount of memory services will stop because they are out of memory.

ps


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 affect.

Resolving: High MySQL Memory Usage


MySQL is relatively memory efficient on install as most memory using 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:

innodb_buffer_pool_size = 16k
key_buffer_size = 16k
myisam_sort_buffer_size = 16k
query_cache_size = 1M


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:

SPAMDOPTIONS="-d -c -m1 –H”

 
  • 37 Users Found This Useful
Was this answer helpful?

Related Articles

How do I install Perl?

via SSH and enter the following: yum -y install perl

How do I backup MySQL in Linux?

1. Copying from the mysql directoryBy default, MySQL databases on servers that use Linux are...

How to Change root/user password?

Via SSH following:   Passwd

Install Config Server Firewall (CSF)?

Installation============Installation is quite straightforward: Login as the root user to SSH and...

Common SSH Commands - Linux Shell Commands

  Navigating in UNIX pwd Shows the full path of the current directory ls...

Powered by WHMCompleteSolution