2

I am running an Ubuntu VPS with 512MB memory. It is just the development server and so literally no traffic. Despite of that, every now and then I keep on getting Memory Exhaustion messages and my entire memory is being used up. I tried to browse various similar questions at the SF but couldn't really get to a scenario where a server with no traffic is consuming memory. Below is an output from top:

An output of ps aux gives as:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.3  23572  1684 ?        Ss   Apr11   0:03 init
root      1177  0.0  0.1  18888  1028 ?        Ss   Apr11   0:01 cron
mysql     1189  0.0  5.6 258344 29680 ?        Ssl  Apr11   1:04 /usr/sbin/mysqld
syslog    1202  0.0  0.1  12536   788 ?        Ss   Apr11   0:01 /sbin/syslogd -u syslog
root      1268  0.0  0.5  49424  2684 ?        Ss   Apr11   0:00 /usr/sbin/sshd -D
root      1305  0.0  0.4  66168  2560 ?        Ss   Apr11   0:15 sendmail: MTA: accepting connections          
root      8062  0.0  0.6  70796  3408 ?        Ss   10:50   0:00 sshd: root@pts/0    
root      8074  0.0  0.4  18160  2224 pts/0    Ss+  10:52   0:00 -bash
root      8134  0.0  0.6  70796  3428 ?        Ss   11:37   0:00 sshd: root@pts/1    
root      8146  0.0  0.4  18160  2232 pts/1    Ss   11:37   0:00 -bash
root      9337  0.0  2.8 270140 15176 ?        Ss   11:49   0:00 /usr/sbin/apache2 -k start
www-data  9341  0.0  1.9 270272 10276 ?        S    11:49   0:00 /usr/sbin/apache2 -k start
root      9346  0.0  0.2  15064  1136 pts/1    R+   11:56   0:00 ps aux
bind     19665  0.0  2.5 142836 13624 ?        Ssl  Apr25   0:00 /usr/sbin/named -u bind

and output of top:

Tasks:  18 total,   1 running,  17 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:    524288k total,   301000k used,   223288k free,        0k buffers
Swap:        0k total,        0k used,        0k free,        0k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                                  
    1 root      15   0 23572 1684 1332 S    0  0.3   0:03.66 init                                                                                     
 1177 root      18   0 18888 1028  796 S    0  0.2   0:01.17 cron                                                                                     
 1189 mysql     18   0  252m  28m 7944 S    0  5.7   1:04.58 mysqld                                                                                   
 1202 syslog    15   0 12536  788  592 S    0  0.2   0:01.95 syslogd                                                                                  
 1268 root      18   0 49424 2684 2120 S    0  0.5   0:00.12 sshd                                                                                     
 1305 root      18   0 66168 2560  880 S    0  0.5   0:15.99 sendmail-mta                                                                             
 8062 root      19   0 70796 3408 2656 S    0  0.7   0:00.04 sshd                                                                                     
 8074 root      15   0 18160 2224 1548 S    0  0.4   0:00.00 bash                                                                                     
 8134 root      15   0 70796 3428 2656 S    0  0.7   0:00.10 sshd                                                                                     
 8146 root      15   0 18160 2232 1556 S    0  0.4   0:00.04 bash                                                                                     
 9337 root      18   0  263m  14m 5736 S    0  2.9   0:00.08 apache2                                                                                  
 9341 www-data  15   0  263m  10m  816 S    0  2.0   0:00.00 apache2                                                                                  
 9397 root      18   0 70796 3408 2656 S    0  0.7   0:00.02 sshd                                                                                     
 9409 root      15   0 12560  928  748 S    0  0.2   0:00.00 sftp-server                                                                              
 9410 root      15   0 70796 3428 2656 S    0  0.7   0:00.01 sshd                                                                                     
 9422 root      18   0 12564  872  668 S    0  0.2   0:00.00 sftp-server                                                                              
 9425 root      15   0 19176 1188  932 R    0  0.2   0:00.00 top                                                                                      
19665 bind      18   0  139m  13m 2152 S    0  2.6   0:00.07 named                                                                                    
1
  • 1
    What have you done to reduce the size of the httpd footprint?
    – symcbean
    May 9, 2012 at 13:20

1 Answer 1

5

Apache2 + PHP + mySQL always uses lots of memory, even for only a small number of connections, out of the box. You have to tune it. You need to limit the number of Apache processes (assuming you're using mpm_prefork) and you need to reduce the memory footprint of mysql by tuning the various caches and other elements.

5-10 concurrent connections to an untuned Apache2 server (prefork mpm), with PHP and mysql, with 512MB of memory, will probably result in it running out of memory briefly.

Plus, your server is running additional processes, like bind and sendmail, which have their own memory requirements.

You're going to have to tune it using the various guides on the web, and other similar questions here on ServerFault.

It's worth saying, this has nothing to do with total hits per day, and everything to do with concurrent sessions to the web server. 20 hits a day all in the same second will kill your server, 20 hits a day spread over 1 hit every hour won't.

Put it this way - if every Apache2 process uses 40MB of memory (and it can easily do so with PHP5 and other modules), you can tolerate about 9 or 10 concurrent connections before your server is using 400MB just for the web server. That leaves you 115MB for everything else.

2
  • You could easily add a swap file, if you don't care about occasional bumps in performance: dd if=/dev/zero of=/swapfile bs=1M count=512, mkswap /swapfile, swapon /swapfile.
    – Oliver
    May 9, 2012 at 13:17
  • Swapfile's probably won't help significantly - because the Linux OOM killer will kill your Apache processes. I speak from experience. Remember, that 512MB is already overcommitted because Linux overcomits memory. The Apache processes all need active memory, and mysql's memory will be active as well, so the only thing in swap will be the low memory footprint stuff that wasn't doing much in the first place. May 9, 2012 at 13:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .