how to programmatically monitor a process memory usage?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
how to programmatically monitor a process memory usage?
I need to obtain the memory usage (real and virtual) of a particular process every couple of seconds and write it to a log file. I'm thinking of writing either shell script (sh or bash) or a C/C++ program. What shell commands and/or C/C++ API's are available for this task?
This displays more than what you have asked, just giving you an idea. You can use this and pipe a grep to search for the process that you want. To capture this every couple of seconds, I would put this in an infinite while loop and sleep for X seconds and run the command.
1) how to get this GetMemUsage script and the program it monitors to start up at the same time? I don't want to start up the program then open a second console (the program writes to stdout) to launch the script. This means I would miss the first second or two of the program.
2) how to get this GetMemUsage script to shut down cleanly and automatically when the program it monitors shutdowns, even it shuts down abnormally? I would like the ability to batch run the program several times.
Also, how to get the elapsed time since the program started (preferrably in milliseconds or better resolution)? other than saving the start time and subtract it from the current time? what if I wanted CPU time and not wall-clock time?
Here's what I've got so far. This is the GetMemUsage script:
Code:
#!/bin/sh
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=0
PERIOD=2 # seconds
# Monitor memory usage forever until script is killed
while :
do
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=""
# Needs to get actual elapsed time instead of doing this
ELAPSED_TIME=`expr $ELAPSED_TIME + $PERIOD`
done
Anyone knows how to get the actual elapsed time of a process (preferrably in milliseconds resolution but seconds resolution is fine)?
I cannot think of any way to have the GetMemUsage script launches and shuts down in synchronization with the monitored process other than to have a launch script that launches and shuts down both processes. Here's the launch script:
How do i capture the memory usage of that particular process running in multiple threads. In linux every thread is displayed in the top statistics.
In solaris, the case is different it shows the number of threads as NLWP but one process.
Any clues. Can we do it using ps but if it list the /proc/pid/status
How can I get the memory consumption of a process in such case. Any ideas and suggestions.
Have you looked at man proc? That has a lot of info regarding how to interpret files in /proc/pid. I think everything you are looking for will be in /proc.
Are you talking about computer shutdown, or program exit when the monitored program exits?
ta0kira
ptobra, what Linux kernel are you using.?
I did some dev in Perl thrs and found that 2.4.x Linux kernels show each thr as a separate process, but 2.6.x kernel shows only 1 process.
iirc, it's due to a change in the underlying kernel code.
ptobra, what Linux kernel are you using.?
I did some dev in Perl thrs and found that 2.4.x Linux kernels show each thr as a separate process, but 2.6.x kernel shows only 1 process.
iirc, it's due to a change in the underlying kernel code.
Threads are shown under /proc/#/task in 2.6 and have most of the same info available as the main process.
ta0kira
If the mem is shared between thrs (it might be/ can't remember), then choose the main/parent thr.
Otherwise, you'd have to sum all thrs mem values.
One way to check is to run a test on a quiet system, and observe the top values when prog is running vs not running.
Incidentally, iirc, only even-num (2.4, 2.6) kernels are prod ready, odd nums (2.5, 2.7) are dev/test ???
I think getting the pid of parent thread and observing is most appropriate.
Summing up all thread mem values and the calculating the average also makes sense.
How much RAM / Memory does a program use. Note that this takes care of parent process etc, mentioned above, and gives usage for a program ( not a process ). so this is really helpful.
I think getting the pid of parent thread and observing is most appropriate.
Summing up all thread mem values and the calculating the average also makes sense.
#!/bin/sh
USAGE="Usage: $0 processName"
if [ $# -ne 1 ]; then
echo $USAGE
exit 1
fi
LOG_FILE="memusage.csv"
echo "ElapsedTime,VmSize,VmRSS" > $LOG_FILE
ELAPSED_TIME=0
PERIOD=1 # seconds
# Monitor memory usage forever until script is killed
while :
do
SUM_VM_SIZE=0
SUM_RSS_SIZE=0
# In case the monitored process has not yet started
# keep searching until its PID is found
PROCESS_PIDS=""
while :
do
PROCESS_PIDS=`/sbin/pidof $1`
if [ "$PROCESS_PIDS.X" != ".X" ]; then
break
fi
done
for PID in ${PROCESS_PIDS} ; do
VM_SIZE=`awk '/VmSize/ {print $2}' < /proc/$PID/status`
if [ "$VM_SIZE.X" = ".X" ]; then
continue
fi
#echo exprVM_ $SUM_VM_SIZE + $VM_SIZE
SUM_VM_SIZE=`expr $SUM_VM_SIZE + $VM_SIZE`
VM_RSS=`awk '/VmRSS/ {print $2}' < /proc/$PID/status`
if [ "$VM_RSS.X" = ".X" ]; then
continue
fi
SUM_RSS_SIZE=`expr $SUM_RSS_SIZE + $VM_RSS`
done
echo "$ELAPSED_TIME sec, $SUM_VM_SIZE KB, $SUM_RSS_SIZE KB"
echo "$ELAPSED_TIME,$SUM_VM_SIZE,$SUM_RSS_SIZE" >> $LOG_FILE
sleep $PERIOD
VM_SIZE=""
VM_RSS=""
# Needs to get actual elapsed time instead of doing this
ELAPSED_TIME=`expr $ELAPSED_TIME + $PERIOD`
done
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.