How to track memory hogs

This is from Eric Harrison on the K12LTSP mailing list:

There is probably a much better way to do this, but here is quick-and-dirty method for watching for memory hogs:

1) run "crontab -e"
2) enter in this line:

* * * * * (echo; date; ps -eo pmem,args,user | sort -nr | head) >> $HOME/memory-pigs.txt

This will create a file in your home directory that contains the top 10 memory hogs every minute. The output will look like this:

Thu Jul 22 14:23:45 PDT 2004
2.8 amavisd (child) vscan
2.7 amavisd (virgin vscan
2.7 amavisd (master) vscan
0.9 ntpd -U ntp -p / ntp
0.8 /usr/sbin/snmpd root
0.4 /usr/bin/perl /u root
0.4 sshd: root@pts/1 root
0.4 smtpd -n smtp -t postfix
0.4 smtpd -n 127.0.0 postfix
0.4 nqmgr -l -n qmgr postfix

In this case, the amavisd is the top memory hog, each process taking up about 2.7% percent of the available memory. Once you are done debugging, run "crontab -e" again and remove the line.