This article shows techniques used to trim memory usage on OpenVZ system (with 128 MB RAM burstable). Mostly inspired by this article from lowendbox.com.
Minimal Debian Lenny install (33MB used):
# ps xo vsz,rsz,ucmd
VSZ RSZ CMD
1980 692 init
26988 1236 rsyslogd
5272 1024 sshd
2036 792 cron
8016 2916 sshd
4324 1632 bash
3604 804 ps
# free -m
total used free shared buffers cached
Mem: 128 33 95 0 0 0
-/+ buffers/cache: 33 95
Swap: 0 0 0
After setting "ulimit -s 128" in /etc/init.d/rc (9MB used):
# ps xo vsz,rsz,ucmd
VSZ RSZ CMD
1980 692 init
2744 1188 rsyslogd
5272 1020 sshd
2036 792 cron
8016 2888 sshd
4324 1628 bash
3604 804 ps
# free -m
total used free shared buffers cached
Mem: 128 9 118 0 0 0
-/+ buffers/cache: 9 118
Swap: 0 0 0
Next operation: replace rsyslogd with syslog-ng (still 9 MB used):
# ps xo vsz,rsz,ucmd
VSZ RSZ CMD
1980 688 init
5272 1040 sshd
2036 792 cron
2848 748 syslog-ng
8016 2892 sshd
4360 1712 bash
3604 808 ps
# free -m
total used free shared buffers cached
Mem: 128 9 118 0 0 0
-/+ buffers/cache: 9 118
Swap: 0 0 0
I replaced bash with dash (smaller memory usage). Free memory remains the same (we keep bash for root interactive login). Bigger benefits come from using dropbear instead of sshd:
# ps xo vsz,rsz,ucmd
VSZ RSZ CMD
1980 688 init
2036 792 cron
2848 748 syslog-ng
2040 468 dropbear
2700 1240 dropbear
2788 1512 bash
2084 704 ps
# free -m
total used free shared buffers cached
Mem: 128 4 123 0 0 0
-/+ buffers/cache: 4 123
Swap: 0 0 0
Yes! we're using only 4MB of VSZ now!.
Next, we would like to install a database, MySQL is a most popular choice here:
# ps xao vsz,rsz,ucmd
VSZ RSZ CMD
1980 688 init
2036 792 cron
2848 808 syslog-ng
2040 468 dropbear
2700 1332 dropbear
2796 1564 bash
1704 516 mysqld_safe
62440 17968 mysqld
1628 532 logger
2084 700 ps
# free -m
total used free shared buffers cached
Mem: 128 61 66 0 0 0
-/+ buffers/cache: 61 66
Swap: 0 0 0
Pretty fat. Let's remove innodb support first:
# ps xao vsz,rsz,ucmd
VSZ RSZ CMD
1980 688 init
2036 792 cron
2848 808 syslog-ng
2040 468 dropbear
2700 1332 dropbear
2804 1572 bash
1704 516 mysqld_safe
44648 7208 mysqld
1628 532 logger
2084 700 ps
# free -m
total used free shared buffers cached
Mem: 128 43 84 0 0 0
-/+ buffers/cache: 43 84
Swap: 0 0 0
After applying minimal settings on my.conf file:
# ps xao vsz,rsz,ucmd
VSZ RSZ CMD
1980 688 init
2036 792 cron
2848 808 syslog-ng
2040 468 dropbear
2700 1332 dropbear
2804 1572 bash
1704 516 mysqld_safe
28256 5320 mysqld
1628 532 logger
2084 708 ps
# free -m
total used free shared buffers cached
Mem: 128 27 100 0 0 0
-/+ buffers/cache: 27 100
Swap: 0 0 0
Now lightpd installation:
# ps xao vsz,rsz,ucmd
VSZ RSZ CMD
1980 688 init
2036 792 cron
2848 808 syslog-ng
2040 468 dropbear
2700 1332 dropbear
2804 1572 bash
1704 516 mysqld_safe
28256 5320 mysqld
1628 532 logger
1764 512 portmap
3136 504 famd
5408 1004 lighttpd
2084 704 ps
# free -m
total used free shared buffers cachedMem: 128 28 99 0 0 0
-/+ buffers/cache: 28 99
Swap: 0 0 0
Not bad. 28 MB used so far.
UPDATE: I found interesting article on optimizing MySQL and added the following options:
query_cache_size = 50K
query_cache_limit = 50K
Memory usage after change:
# ps xao vsz,rsz,ucmd
VSZ RSZ CMD
1980 688 init
1764 512 portmap
2796 1248 rsyslogd
5404 1008 lighttpd
3136 500 famd
2036 796 cron
2040 468 dropbear
2696 1340 dropbear
2804 1576 bash
1704 516 mysqld_safe
11436 5112 mysqld
1628 532 logger
2084 700 ps
# free -m
total used free shared buffers cached
Mem: 128 12 115 0 0 0
-/+ buffers/cache: 12 115
Swap: 0 0 0
12 MB VSZ with lightpd and MySQL! :-)