How to monitor the memory consumed by a process?

781

Solution 1

Method 1

Run:

top

Check for the program's PID (first column), then run:

top -p PID

Method 2

Either paste this into the terminal or save it as a mem_usage.sh and run it from terminal.

#! /bin/bash
while :
do
    clear
    ps faux | grep casper
    sleep 1s
done

Solution 2

To monitor only your process you can check /proc/PID/status or /proc/PID/statm.

About /proc/PID/statm:

After doing cat /proc/PID/statm you should see this:

611450 185001 883 18 0 593431 0

Explanation:

  1. size :- total program size (611450 X 4096/1024 = 2445800kB = 2388M)
  2. resident :- resident set size (185001 X 4096/1024 = 740004kB = 722M)
  3. share :- shared pages (883 X 4096 = 3532)
  4. trs :- text (code) (18 X 4096/1024 = 72kB = VmExe )
  5. drs :- data/stack
  6. lrs :- library (593431 X 4096/1024 = 2373724kB = VmData +VmStk)
  7. dt :- dirty pages

Also you can log the memory activity for your process doing a loop using date and cat.

Solution 3

you could use use top

man top

This program allows you to sort the resource usage by, amongst others, RSS, VSZ, CPU, etc... It's very useful.

Alternatively, for a more detailed breakdown of memory usage, try 'pmap'

man pmap

Example usage:

pmap -x 1234

Solution 4

Open System Monitor, and go to the Processes tab:

Solution 5

Give this a try:

watch 'ps faux | grep -v grep | grep casper'

You may also change refresh interval using the --interval <seconds> parameter.

Share:
781

Related videos on Youtube

John Boe
Author by

John Boe

Updated on September 18, 2022

Comments

  • John Boe
    John Boe over 1 year

    How to check if property cssRules exists in document.styleSheets[i] object?

    I have found out that I cannot use

    if ( sheets[i].hasOwnProperty("cssRules") )
      because .cssRules is inherited property.
    

    But when I try to use

    if( sheets[i].cssRules !== undefined )
    

    so in debugger (Firefox 48 Toolbox tool) I got exception: SecurityError.

    For this reason the code fails.

    var cssList = function(node) {
        var sheets = document.styleSheets, o = {};
        var sheet;
        for (var i in sheets) {
          if( sheets[i].cssRules !== undefined )
            sheet = sheets[i].cssRules;
          else
          if( sheets[i].rules !== undefined )
            sheet = sheets[i].rules;
          else
            continue;
    
          var rules = sheets[i].rules || sheets[i].cssRules;
        }
        return o;
    }
    
    • CBroe
      CBroe over 7 years
      If you’re getting a security error, then that is probably a cross-domain issue – is the stylesheet in question being loaded from a different domain?
    • John Boe
      John Boe over 7 years
      Not the stylesheet. The script is injected from Firefox webextensions (addon). So the web-page is remote and script is on client side. But the webextensions works like that it pretends to be part of the page. Maybe it is some security policy of the Firefox addons.
  • Wilf
    Wilf over 10 years
    How can you change it from showing %? Out of interest :-)
  • Wilf
    Wilf over 10 years
    With pmap -x PID, did you mean incredibly detailed usage :-)
  • swisscheese
    swisscheese over 10 years
    indeed - can never have enough detail :)
  • Hommer Smith
    Hommer Smith over 10 years
    I get this when running the script: bash: ./memory_usage.sh: bin/bash: bad interpreter: No such file or directory
  • Julian Stirling
    Julian Stirling over 10 years
    Bah! I have edited it! Should have checked it properly before posting. The first line should say where to run it from /bin/bash (I had bin/bash so it was checking locally in a folder called bin for bash)
  • Julian Stirling
    Julian Stirling over 10 years
    @ wilf ps faux | grep casper | awk '{print $11} {print $6}' That should give you on one line the program name and the next the real memory size in kB.
  • John Boe
    John Boe over 7 years
    The code is in Firefox webextensions. It is Content Script injected into web-page (a blog). "cssRules" in sheet also was not working.
  • the8472
    the8472 over 7 years
    then it's probably a cross-domain <link>?
  • John Boe
    John Boe over 7 years
    Hey, you are genius. Thanks a lot! The catch ... try works!
  • Wudang
    Wudang almost 5 years
    You don't need the -v grep if you change the final one to grep [c]asper. That matches casper but not itself.
  • orrd
    orrd over 4 years
    On mine (Ubuntu 16.04.5 LTS) "pmap -x" with the PID doesn't show anything other than the PID and the command. -X does the same.