top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

How to know the CPU and Memory usage (in terms of %) of a Process in Linux?

+5 votes
How to know the CPU and Memory usage (in terms of %) of a Process in Linux?
posted Jan 8, 2014 by Salil Agrawal

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

3 Answers

+2 votes

We can get the %CPU and %MEM usage of particular process using ps command.
Give the proper process id for that particular process.

Below is the sample output.

# ps -p 4599,1 --sort -rss -o comm,pmem,pcpu
COMMAND         %MEM       %CPU
dre_app          0.2       0.4
init             0.0       0.0

Other commands also available which gives the cpu, memory details which are top, mpstat (for cpu), pmap (for mem)

answer Feb 6, 2014 by Gurpreet Singh Matharoo
+1 vote

You need to parse out the data from /proc//stat. These are the first few fields (from Documentation/filesystems/proc.txt in your kernel source):

Contents of the stat files.

Field : Content

pid: process id
tcomm: filename of the executable
state : state (R is running, S is sleeping, D is sleeping in an
uninterruptible wait, Z is zombie, T is traced or stopped)
ppid: process id of the parent process
pgrp : pgrp of the process
sid : session id
tty_nr : tty the process uses
tty_pgrp: pgrp of the tty
flags : task flags
min_flt : number of minor faults
cmin_flt : number of minor faults with child's
maj_flt : number of major faults
cmaj_flt : number of major faults with child's
utime : user mode jiffies
stime : kernel mode jiffies
cutime : user mode jiffies with child's
cstime kernel mode jiffies with child's

You're probably after utime and/or stime. You'll also need to read the cpu line from /proc/stat, which looks like:

'cpu 192369 7119 480152 122044337 14142 9937 26747 0 0'

This tells you the cumulative CPU time that's been used in various categories, in units of jiffies. You need to take the sum of the values on this line to get a time_total measure.

Read both utime and stime for the process you're interested in, and read time_total from /proc/stat. Then sleep for a second or so, and read them all again. You can now calculate the CPU usage of the process over the sampling time, with:

user_util = 100 * (utime_after - utime_before) / (time_total_after - time_total_before);
sys_util = 100 * (stime_after - stime_before) / (time_total_after - time_total_before);

answer Jan 8, 2014 by Sandeep
+1 vote

Please use top command to get all details of CPU and memory usages like below column:


answer Jan 14, 2014 by Amit Kumar Pandey
Similar Questions
+2 votes

I want a script which -
1. Monitor is list of process in Linux.
2. When it is any process is down it can log the error and restart the process.
3. List of the processes can be supplied as the command line parameters.

+4 votes

Code to write data in to file example.txt.. which is working fine

// basic file operations
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  ofstream myfile; ("example.txt");
  myfile << "Writing this to a file.\n";
  myfile << "Writing this to a file.\n";
  myfile << "Writing this to a file.\n";
  myfile << "Writing this to a file.\n";
  myfile << "Writing this to a file.\n";
  myfile << "Writing this to a file.\n";
  return 0;

File : filelimit.c below which takes input as the compiled binary of above code
In this code i am setting the file size limit of process to 10 bytes and the above code is trying to write more than 10 bytes. According to below code if a process is trying to write more than 10 bytes to file it should send the signal SIGXFSZ (see more about this here)

It is not sending here... Why??

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>

void handler(int sig)
    if(sig == SIGXFSZ)
        printf("Inside handler with correct signal\n");
        printf("Inside handler with wrong signal\n");

int main(int argc, char const *argv[])
    pid_t pid;
    pid = fork();

    if(pid == 0)
        signal(SIGXFSZ, handler);

        char command[256];
        scanf("%s", command);

        int ret;

        struct rlimit rl;
        rl.rlim_cur = 10;
        rl.rlim_max = 10;
        setrlimit (RLIMIT_FSIZE, &rl);

        char *envp[] = { NULL };
        char *argv[] = { command , NULL };
        ret = execve(command, argv, envp);

        printf("%d - %d\n", getpid(), pid);
        signal(SIGXFSZ, handler);
    return 0;
+2 votes

I have to write a python script which has to give CPU usage for particular process.
Ex: Lets say i have a process ABC which is running continuously for 10 hours.(Process may have 'n' number of threads.)
1st) My script should have to check the CPU usage and Memory consumption for process ABC, and also for threads of it after every 5 seconds.

2nd) Script has to take an average of CPU usage and memory consumption and it has to print final result.

i have tried using this command ,

top -bn1 | grep "Cpu(s)" | \
           sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | \
           awk '{print 100 - $1"%"}'

Can anyone help in find out?

+1 vote

I would like to use Perl on an embedded device, which only has 64MB of RAM.
Are there any tricks to reduce the memory usage of the perl interpreter?

Contact Us
+91 9880187415
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Karnataka INDIA.