Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I'm trying to measure the memory of a process that I call via the command line (i.e., I want to find out much RAM it takes to for the process ). Is there any command that I can add to the command calling the process that will achieve this?

share|improve this question
2  
simple answser is htop, isntall it by sudo apt-get install htop btw this is a dupe of a question just haven't got the time to find it now... –  Alvar May 7 '13 at 21:57
add comment

2 Answers 2

Process Memory

Example of firefox

 ps -aux | grep -i firefox

Shows you the pid of firefox process, Then you can use top -p pid

 top -p  3845

enter image description here


You can also use ps command, firefox pid is 3845

 ps -p 3845 -o %cpu,%mem,cmd
 %CPU  %MEM CMD
 11.1  3.7 /usr/lib/firefox/firefox

The truth is that i am not satisfied with the above mentioned commands, do not know why... so i start googling and after 3 hours i found something that you should be interested in..

Monit

 sudo apt-get install monit -y

Edit the Monit Config File

 sudo nano /etc/monit/monitrc

Enable the web interface

 set httpd port 2812
 # use address localhost # only accept connection from localhost
 allow localhost # allow localhost to connect to the server and
 # allow 192.168.1.0/255.255.255.0 # allow any host on 192.168.1.* subnet
 allow admin:monit # require user 'admin' with password 'monit'

Checking process every 2 secons

 ## Start Monit in the background (run as a daemon):
 set daemon 120 to only 2  # check process every 2 sec

Example Firefox

In the end copy paste the following command

    check process firefox
    matching "firefox"

Save and Exit

Check your syntax

Fix any problems found – it’s not too tough to figure out what’s going on.

 sudo monit -t

Start (or restart) Monit

 sudo service monit start

Visit the web interface

 http://localhost:2812 if you’re running Ubuntu Desktop, or

Sign in with your admin:monit credentials

enter image description here

Click on Firefox

enter image description here

I hope this will help you

Helpful Links You should check

How to Install Monit

Monit: check process without pidfile

http://mmonit.com/wiki/Monit/FAQ

Real-world Monit configuration examples

How can I get the CPU usage and memory usage of a single process on Linux (Ubuntu)?

You can also use these links for help and modify your process.


UPDATE

You can also configure an alert if firefox uses more than 250 MB of ram

 check process firefox
 matching "firefox"
 if totalmem > 250.0 MB for 1 cycles then alert

enter image description here

enter image description here

You can also execute command

 if totalmem > 250.0 MB for 1 cycles then exec "path to script"

You can also make a script of Notify-Send

 /usr/bin/notify-send firefox "More Than 250 MB OF RAM"

enter image description here

I just run it from terminal cauzz my script is not working, so if any would make a script please share with us

Now you have an idea make it possible

share|improve this answer
add comment

The Gnu time command can print the maximum resident set size used by a command. You do have to make sure to use the /usr/bin/time command, not the Bash Shell built-in time keyword.

For an example to measure the firefox command:

/usr/bin/time --format="Size:%MK  Cpu:%P  Elapsed:%e" firefox &

After using firefox a while, I close it out to get the report:

Size:168644K  Cpu:30%  Elapsed:226.34

While it is possible to use the TIME environment variable to set the default format I've found it more flexible to set up individual bash aliases with specific formats. So for the above I would add to my ~/.bash_aliases file:

alias ztm="/usr/bin/time --format=\"Size:%MK  Cpu:%P  Elapsed:%e\""

So that from my Bash Shell I could just enter:

ztm firefox &

References:

  • man time
  • info time
  • man getrusage # - Shows which measures are available on Linux, the others show as zero
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.