Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Write an article for LinuxForums Today! Win Great Prizes!
Find the answer to your Linux question:
New to Linux Forums? Register here for free!
    Linux Forums > Your Distro > Redhat / Fedora Linux Help > how to programmatically monitor a process memory usage?

Forgot Password?
 Redhat / Fedora Linux Help   Help and discussion related to Redhat and Fedora Linux.

Site Navigation
Linux Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
RSS Feeds
Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Closed Thread
 
Thread Tools Display Modes
Old 11-26-2005   #11 (permalink)
Just Joined!
 
Join Date: Jun 2005
Location: Blackburn, North West England (UK)
Posts: 50
Sorry about another post so quickly but in this new modification the script shutsdown automatically on process death.
Code:
#!/bin/sh
#GetmemUsageModified2

USAGE="Usage: $0 processName"

if [ $# -ne 1 ]; then
   echo $USAGE
   exit 1
fi

# In case the monitored process has not yet started
# keep searching until its PID is found
PROCESS_PID=""
while :
do
   PROCESS_PID=`/sbin/pidof $1`

   if [ "$PROCESS_PID.X" != ".X" ]; then
      break
   fi
done

LOG_FILE="memusage.csv"

echo "ElapsedTime,VmSize,VmRSS" > $LOG_FILE

ELAPSED_TIME=`date`
PERIOD=2        # seconds

while :
do
 if [ -d /proc/$PROCESS_PID ] ; then
   VM_SIZE=`awk '/VmSize/ {print $2}' < /proc/$PROCESS_PID/status`
   if [ "$VM_SIZE.X" = ".X" ]; then
      continue
   fi
   VM_RSS=`awk '/VmRSS/ {print $2}' < /proc/$PROCESS_PID/status`
   if [ "$VM_RSS.X" = ".X" ]; then
      continue
   fi
   echo "$ELAPSED_TIME,$VM_SIZE,$VM_RSS" >> $LOG_FILE
   sleep $PERIOD
   VM_SIZE=""
   VM_RSS=""
   ELAPSED_TIME=`date +%H:%M:%S:%N`
 else
 echo "$1 is no longer a running process"
 exit 0
 fi
done
I wont post again untill i recive a reply so i know you have gotten this, then after that i'll just edit this post with the new script to prevent this thread from becoming huge.
skrye is offline  



Old 11-27-2005   #12 (permalink)
Just Joined!
 
Join Date: Jun 2005
Posts: 23
skrye,

I'm a developer and this is my attempt at a simple memory usage profiling for an application I'm testing:

1) I was aiming for a _relative_ elapsed time, i.e. number of milliseconds since the start of the process, not absolute time. (This makes for simple graphing and comparison across multiple runs and versions.)

2) Running GetMemUsage as a daemon certainly has its advantages but requires root privileges, and that's not always possible. However, please post your solution so I can see if I can adapt it to my needs.

My original thinking is this:
a1) write a launch script and names it the same as the monitored process (of course, rename the monitored process to something else), thus achieving transparency. (This means a different launch script for each monitored process. Since I'm a developer interested in a few particular applications, this is fine.) Or,
a2) pass the name of the monitored process as an argument to the launch script
b) This launch script, running as a user process, would first launch GetMemUsage in the background, then launch the monitored process in the foreground. The launch script would end when the monitored process ends (either normally or interrupted via Ctrl-C, which in my case, happens quite frequently). When the launch script ends, it would end the GetMemUsage script, preferably cleanly (so far I can only think of kill -9), as part of its cleanup.

Looking forward to seeing your solutions.
thanhvn is offline  
Old 11-27-2005   #13 (permalink)
Just Joined!
 
Join Date: Jun 2005
Location: Blackburn, North West England (UK)
Posts: 50
Ok i think i we got the launch script covered, take a look at this and see what you think.
Code:
#!/bin/sh
#
#GetMemUsage launch script

cmd=$1

if [ -n $cmd ] ; then
  ./GetMemUsageModified.sh $cmd &
  $cmd
  wait
fi
if ps x | perl -ne 'print if /\b'$cmd'\b/' > /dev/null 2>&1 ; then
wait
else
exit 0
fi
I'm trying to utilize the time command to resolve to the relative elapsed time issue. If you run this in debug mode on the modified GetMemUsage you shall see that its simple but effcient.
skrye is offline  
Old 01-30-2006   #14 (permalink)
Just Joined!
 
Join Date: Jan 2006
Posts: 1
portable `pidof` location

w.r.t. GetMemUsg: my debian system has `/bin/pidof` and not `/sbin/pidof`

so add this line:
Code:
PIDOF_CMD=`which pidof`
and perform following replacement:
Code:
# replace this: PROCESS_PID=`/sbin/pidof $1`
# with this:
PROCESS_PID=`$PIDOF_CMD $1`
perhaps you can just try to call `pidof` instead of assigning the result of `which pidof`

also, if you want to monitor scripts too (e.g. ruby, perl, etc), add this line instead
Code:
PIDOF_CMD="`which pidof` -x"
Nwallins is offline  
Old 03-08-2007   #15 (permalink)
Just Joined!
 
Join Date: Mar 2007
Posts: 1
Dear Guys,

I searched on the net for monitor memory usage and I got a very good solution here.

Thank you very much.

Regards,
shathy
shathy is offline  
Old 09-09-2007   #16 (permalink)
Just Joined!
 
Join Date: Sep 2007
Posts: 2
This is awsome andf i am sure will help u out.. chekc this guys

First of all thanks, this thread was really helpful.

Even i am a developer, and I found this link to be immensely useful. Awsome and accurate memory profiling can be done using these.

Check them out.

Per process accurate memory usage , private region , public region and shared library page mapping included. better than VMSIZE and RSS values.

Memory usage Retrieval on Linux ( process wise and general )

How much RAM / Memory does a program use.

How much RAM is used per program .

Regards,

-- Ritu
Quote:
Originally Posted by shathy View Post
Dear Guys,

I searched on the net for monitor memory usage and I got a very good solution here.

Thank you very much.

Regards,
shathy
ritu_singhal is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Free Magazines
Run Your Own Web Server Using Linux & Apache - Free 191 Page Preview
Learn about everything you'll need to build and maintain your Linux servers, and to deploy Web applications to them.
subscribe
Open Source Security Myths Dispelled
Dispel the five major myths surrounding Open Source Security and gain the tools necessary to make a truly informed decision for your IT organization
subscribe
InformationWeek
InformationWeek is the only newsweekly you'll need to stay on top of the latest developments in information technology.
subscribe



All times are GMT. The time now is 01:30 PM.






© 2000 - - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.3.1