Reducing memory usage
Memory is cheap. More than that it's also one of the most cost-effective ways to increase the performance of an application or server.
However virtual machines (particularly on hosted sites) are often charged according to memory usage so it is still useful to be able to trim your usage.
Here are some steps we took to reduce the usage on our virtual machines.
Tools
The main tool I use is "ps aux" - the columns of interest are "VSZ" and "RSS". VSZ is the virtual size of a program - how much memory has been allocated to it. This number is often quite large - however it's not as useful as the RSS number. The Resident Set Size is the amount of "physical" memory that process is using. If a program asks for 1GB of memory but only writes to 100MB of that, VSZ will say 1GB, RSS will say 100MB. Well almost, if 20MB of that 100MB was swapped to disk, RSS will say 80MB. Overall however, RSS gives a better indication of memory "pressure".
That leads us nicely to "free" - this will show you the amount of swap used - which may help determine how accurate the RSS figure is. Lastly "vmstat 1" will show you how much you are reading/writing to swap in the si/so - swap in (reading), swap out (writing) columns.
32 or 64 bit?
There is an excellent article by
David N. Welton about this subject. Different languages and applications have different levels of bloat running in a 64-bit userspace compared to 32-bit userspace. However most people do not need 64-bit userspace (large databases and particularly Java programs perhaps being the exceptions).
Reducing number of programs
It is a given that stopping any unnecessary programs is a good idea, however you may also want to reduce the number of copies of a given program. This is useful for some programs which start multiple copies of themselves in order to increase performance - apache, dovecot and amavis are examples.
The savings may not be as much as you expect however. Unix uses Copy-on-write - the result being that since most of these programs are near identical copies of each other, each additional copy only uses a bit more memory - not double as one might expect. It is probably a good idea none the less to keep the numbers low - this way you won't find that you are running out of memory - instead you will suffer from a performance bottleneck. Running out of memory would also result in performance problems, but these would affect the entire server rather than a specific application and could result in programs crashing or being killed off.
For amavis (an anti-spam, anti-virus program), create a file /etc/amavis/conf.d/51-lowmem with the contents:
$max_servers = 1;
For dovecot (an IMAP, POP3 server for email), only enable the protocols you are using by editing the "protocols" line in /etc/dovecot/dovecot.conf. The default is "imap imaps pop3 pop3s". For example we only use "imaps". Then reduce "login_process_count" or even set "login_process_per_connection" to "no".
Apache
To shrink Apache's usage it's easiest to follow these instructions. When disabling modules, do check that you are not breaking your website - apache will need a full restart (not just a reload/graceful command) in order to remove modules (or indeed to add them).
MySQL
A great guide to reducing MySQL memory usage is available here. To find out if you are using InnoDB, run this command in MySQL:
select TABLE_SCHEMA,TABLE_NAME,DATA_LENGTH from information_schema.tables where ENGINE="InnoDB";
We were not using InnoDB, just disabling the InnoDB changed the VSZ/RSS usage of MySQL from 128MB/18MB to 53MB/9MB respectively - a reduction of more than 50%.
ClamAV
We use ClamAV for virus scanning. After startup, clamd v0.92.1 was using 77MB/66MB of memory (VSZ/RSS), however 10 days later this had increased to 234MB/162MB. Later versions of ClamAV claimed to ahve improved this situation. Using 0.95.1 after a restart 87MB/67MB was in use - if anything slightly worse, however 16 days later, it was still only using 87MB/68MB - a huge improvement on the earlier version.