Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I find out if I have a memory leak and if so, what is causing it?

Troubleshooting

How can I find out if I have a memory leak and if so, what is causing it?

by  cspilman  Posted    (Edited  )
This script will give a pretty good idea if you have memory leaks and what might be causing it. It's not the prettiest and can probably be simplified but it does what it was designed to do, find memory leaks if you have them.

This has been tested to work on AIX 6.1 and below:

Code:
#!/bin/ksh

echo "\n\nPlease wait..."

if [ ! -f /tmp/ps.before ] ; then
        ps -e -o pid,vsz,ruser,etime,args | awk '!/defunct/{print $1","$2","$3","$4","$5,$6,$7}' > /tmp/ps.before 
fi

ps -e -o pid,vsz,ruser,etime,args | awk '!/defunct/{print $1","$2","$3","$4","$5,$6,$7}' > /tmp/ps.after

cat /tmp/ps.before /tmp/ps.after > /tmp/ps.total

awk -F, '! /PID/{print $1,$2,$3,$4,$5,$6,$7}' /tmp/ps.before | while read pid vsz ruser etime command a b
do
       num_procs=`grep -c "^${pid}," /tmp/ps.total`
       if [ "${num_procs}" -eq 2 ] ; then
               after_vsz=`awk -F, '/'"^${pid},"'/{print $2}' /tmp/ps.before`
               ((delta=vsz-after_vsz))
               echo "${pid}\t${vsz}\t\t${after_vsz}\t\t${delta}\t${ruser}\t${etime}\t${command} $a $b"
       fi
done > /tmp/ps.after.tmp

#clear
echo "PID\tVSZ SIZE BEFORE\tVSZ SIZE AFTER\tDELTA\tUSER\tELAPSED TIME\tCOMMAND"
echo "---\t---------------\t--------------\t-----\t----\t------------\t-------"
sort +3 /tmp/ps.after.tmp

VSB=`awk '{ sum += $2 } END { print "Total VSZ Before:                ", sum }' /tmp/ps.after.tmp`
VSA=`awk '{ sum += $3 } END { print "Total VSZ After:         ", sum }' /tmp/ps.after.tmp`
DELTA=`awk '{ sum += $4 } END { print "Total Delta:                   ", sum }' /tmp/ps.after.tmp`

rm /tmp/ps.after*
rm /tmp/ps.total

start_date=`ls -la /tmp/ps.before | awk '{print $6,$7}'`

echo "\n\nRunning against ps.before file with date of:  ${start_date}"
echo "\n\n${VSB}"
echo "${VSA}"
echo "${DELTA}"
echo "Total Defunct Processes Found:    `ps -ef | grep -c [d]efunct`"
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top