How to find the memory consumption of a particular process in linux for every 5 seconds

82,020

Solution 1

you may use SNMP to get the memory and cpu usage of a process in a particular device in network :)

Requirements:

  • the device running the process should have snmp installed and running
  • snmp should be configured to accept requests from where you will run the script below(it may be configured in snmpd.conf)
  • you should know the process id(pid) of the process you want to monitor

Notes:

  • HOST-RESOURCES-MIB::hrSWRunPerfCPU is the number of centi-seconds of the total system's CPU resources consumed by this process. Note that on a multi-processor system, this value may increment by more than one centi-second in one centi-second of real (wall clock) time.

  • HOST-RESOURCES-MIB::hrSWRunPerfMem is the total amount of real system memory allocated to this process.

**

Process monitoring script:

**

echo "IP: "
read ip
echo "specfiy pid: "
read pid
echo "interval in seconds:"
read interval

while [ 1 ]
do
    date
    snmpget -v2c -c public $ip HOST-RESOURCES-MIB::hrSWRunPerfCPU.$pid
    snmpget -v2c -c public $ip HOST-RESOURCES-MIB::hrSWRunPerfMem.$pid
    sleep $interval;
done

Solution 2

Use top -p PID where PID is the process ID. Information about the process should be displayed, including percent of system memory used. Type d and an integer in seconds to change the refresh rate.

Solution 3

use watch to periodically execute your script. here is an example:

watch 'cat /proc/status' --interval=5

watch 'ps aux' --interval=5

Solution 4

This previously posted question:

How to measure actual memory usage of an application or process?

seems like it may thoroughly address your question.

edit: My personal favorite Linux utility for checking the resource usage of processes is top, though it can be misleading for reasons that are explained in the question I linked.

Share:
82,020
Vamsi
Author by

Vamsi

Updated on July 16, 2022

Comments

  • Vamsi
    Vamsi almost 2 years

    I just want to know how to find the memory consumption of a particular process for particular time(say 5 seconds)

    I am new to linux. So, detailed steps of doing that will be appreciated

  • Vamsi
    Vamsi over 11 years
    in the reply that you have given it is found that we can see process and their starting time, cpu usage and all. In addition to that i want to check the process memory usage for 5 seconds
  • mdunsmuir
    mdunsmuir over 11 years
    Do you mean the change in memory usage over that period? My first instinct might be to write a simple script to run a command that outputs a process's memory usage every 5 seconds, and continuously compute the delta.
  • Vamsi
    Vamsi over 11 years
    if i want to check for a particular process say pid = 12468 ?
  • Vamsi
    Vamsi over 11 years
    Presently i am using this piece to get the CPU usage and the other info of a particular process. unix95= ps -p 12994 -o pid,sz,pcpu,ruser,args,time,stime,pmem,etime I want to check this for every time interval of 5 seconds
  • 0x90
    0x90 over 11 years
    use cat /proc/pid/status or whatever
  • dward
    dward over 11 years
    Regardless of the watch interval, ps u -p PID will not refresh it's values as fast as top
  • Basile Starynkevitch
    Basile Starynkevitch over 11 years
    Or use cat /proc/12488/maps if you want a better understanding of the address space of process 12488...
  • Roselyn Verbo Domingo
    Roselyn Verbo Domingo about 9 years
    memory output will be in Kbytes
  • Bharat Kul Ratan
    Bharat Kul Ratan about 8 years
    For retrieving PID of an process, one can use ps -aux | grep "process" where "process" is the name of process you are looking for.