Search This Blog

Tuesday, May 3, 2011

How to track Application causing Memory Leak using ps command ?

Normally Most of applications uses some part of memory and release it after their operation is over or Application is closed.

However some applications don't just give up memory and leads to memory being utilized heavily.

For RAM consumption simulation i have used a script ( source : http://stackoverflow.com/questions/4964799/write-a-bash-shell-script-that-consumes-a-constant-amount-of-ram-for-a-user-defin )

#!/bin/bash

echo "Provide sleep time in the form of NUMBER[SUFFIX]"
echo " SUFFIX may be 's' for seconds (default), 'm' for minutes,"
echo " 'h' for hours, or 'd' for days."
read -p "> " delay

echo "begin allocating memory..."
for index in $(seq 1000); do
value=$(seq -w -s '' $index $(($index + 100000)))
eval array$index=$value
done
echo "...end allocating memory"

echo "sleeping for $delay"
sleep $delay

saved the script with name /tmp/ramload.sh

As per the script owner it had consumed 570M to 575M physical memory* for the specified time period of 5 minutes.

Ran the command.
# ./ramload.sh
Provide sleep time in the form of NUMBER[SUFFIX]
SUFFIX may be 's' for seconds (default), 'm' for minutes,
'h' for hours, or 'd' for days.
> 5m
begin allocating memory...


You can track such applications using following ps command.

#ps aux --sort rss
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 2 0.0 0.0 0 0 ? S< 01:54 0:00 [migration/0]
root 3 0.0 0.0 0 0 ? SN 01:54 0:00 [ksoftirqd/0]
root 4 0.0 0.0 0 0 ? S< 01:54 0:00 [watchdog/0]
root 5 0.0 0.0 0 0 ? S< 01:54 0:00 [migration/1]
root 4732 0.0 0.4 10732 4848 tty7 Ss+ 01:55 0:01 /usr/bin/Xorg :
root 4746 0.0 0.9 24444 9784 ? SN 01:55 0:00 /usr/bin/python
gdm 4756 0.0 1.5 30488 15540 ? Ss 01:55 0:00 /usr/libexec/gd
root 8900 55.9 2.7 32188 28584 pts/1 R+ 23:58 0:25 /bin/bash ./ramload.sh



Run the command after some time.

USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 4731 0.0 0.3 27392 3740 ? Sl May03 0:00 /usr/libexec/gdm-rh-security-token-helper
root 4318 0.0 0.4 13256 4692 ? S May03 0:00 python ./hpssd.py
root 4732 0.0 0.4 10732 4848 tty7 Ss+ May03 0:01 /usr/bin/Xorg :0 -br -audit 0 -auth /var/gdm/:0.Xauth -nolist
root 4746 0.0 0.9 24444 9784 ? SN May03 0:00 /usr/bin/python -tt /usr/sbin/yum-updatesd
gdm 4756 0.0 1.5 30488 15540 ? Ss May03 0:00 /usr/libexec/gdmgreeter
root 8900 69.5 5.5 60624 57276 pts/1 S+ May03 0:54 /bin/bash ./ramload.sh


You can observe the last entry which is
/bin/bash ./ramload.sh
Is a reason behind memory utilization.

(
Note:
1) Only few entries are displayed from output due to its size.
2) Normally ps aux --sort pmem is used for sorting the ps output using the memory but it has some bug .
see below link for more info
http://www.linuxquestions.org/questions/showthread.php?p=4344941&posted=1#post4344941
)
From above output we can say that
./ramload.sh
process is using the max memory.You need to fire the command at regular interval to check if the process is culprit behind memory crisis.


No comments:

Post a Comment