How to bring down / optimize memory usage in your unmanaged Linux VPS box and avoid OOM (Out Of Memory) errors?

by Yang Yang on July 1, 2010

The other day I was very upset about some extraordinary down times of my unmanaged VPS box at Linode. As it’s unmanaged, support staff at Linode are not responsible for the failures. I contacted them and they told me it’s OOM (Out Of Memory), pointing me to the right documentation to figure out how to get the problem sorted out myself. After a few tweaks and observations for a week, so it seems that I have successfully optimized my VPS server to take on more traffic with less resources such as RAM.

The problem almost always lies in where the user is free to feed stuff to your website or program. Sometimes Convert Hub spikes in memory usage and forces my box to use swap that relies on disk I/O to work. This happens when someone uploads an ultra large picture to be processed or converted. While I may restrict the size of the picture that is allowed to be uploaded, I may also do the following settings to optimize the entire LAMP environment so the other websites enjoy it as well.

Apache 2 Low-Memory Optimization

Use this command to identify the MPM you are using:

apache2 -V | grep 'MPM' # for Debian-based systems
httpd -V | grep 'MPM' # for Fedora/CentOS systems

Find and change these settings in your Apache 2 configuration file (usually found at /etc/apache2/apache2.conf):

StartServers 1
MinSpareServers 3
MaxSpareServers 6
ServerLimit 24
MaxClients 24
MaxRequestsPerChild 3000

Switch to Lighttpd or Litespeed if possible.

MySQL Low-Memory Optimization

Same as above, find and change these settings of the MySQL configuration file (may be at /etc/mysql/my.cnf) accordingly:

key_buffer = 16K
max_allowed_packet = 1M
thread_stack = 64K
table_cache = 4
sort_buffer = 64K
net_buffer_length = 2K

PHP Low-Memory Optimization

Find your PHP configuration file (php.ini) and modify the PHP script memory limit to 32M or less (default is 128M):

memory_limit = 32M

Comments on this entry are closed.

Previous post:

Next post: