Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In Linux, how can I display memory usage of each process if I do a ps -ef? I would like to see the 'virtual memory', 'res memory', 'shared memory' of each progress. I can get that via top, but I want the same info in ps -ef so that I can pipe the output to grep with my process name.

share|improve this question
up vote 7 down vote accepted

Obtaining memory usage through ps is pretty unreliable. If you have a newer kernel it should support /proc/pid#/smaps which gives you some detailed information on each processes memory usage. Below is a pretty dirty and quick script to loop through each process that is open and grab the Size, Rss, Pss and Shared Clean/Dirty usage. Hopefully it can be useful in some kind of way.

#!/bin/bash

for pid in $(ps -ef | awk '{print $2}'); do
    if [ -f /proc/$pid/smaps ]; then
            echo "* Mem usage for PID $pid"
            echo "-- Size:"
            cat /proc/$pid/smaps | grep -m 1 -e ^Size: | awk '{print $2}'
            echo "-- Rss:"
            cat /proc/$pid/smaps | grep -m 1 -e ^Rss: | awk '{print $2}'
            echo "-- Pss:"
            cat /proc/$pid/smaps | grep -m 1 -e ^Pss: | awk '{print $2}'
            echo "Shared Clean"
            cat /proc/$pid/smaps | grep -m 1 -e '^Shared_Clean:' | awk '{print $2}'
            echo "Shared Dirty"
            cat /proc/$pid/smaps | grep -m 1 -e '^Shared Dirty:' | awk '{print $2}'
    fi
done
share|improve this answer
    
You should be summing the Size column. See ezsmith's answer below. – Basil A Jan 10 at 17:23

ps ef -o command,vsize,rss,%mem,size

I could not find an option for shared memory, but I did find options for % of total physical memory and the amount of swapspace that would be needed to swap out the process. This and much more is documented in the man page for ps.

share|improve this answer
    
Linux accounts shared memory in the memory for each process, which can lead to bogus sums if you have lots of processes attached to one largish shared memory segment. top, ps, and similar programs are all affected by this in the same way. – Peter Eisentraut Jan 29 '10 at 8:24
    
I know how shared memory works, but it appears ps cannot provide that information (or I was not able to find it in my ps documentation, at least). top or htop actually can show shared memory usage on a per process basis. – Justin Smith Jan 29 '10 at 13:52

List processes by memory usage

ps -e -orss=,args= | sort -b -k1,1n

share|improve this answer
    
Use ps -e -orss=,pid=,args= | sort -b -k1,1n to include the process id – Jacob Feb 16 '14 at 22:45

you can use

top -n 1
share|improve this answer

@user26528's answer doesn't quite calculate the memory correctly - you need the sum of the mappings in smaps, not just the top one. This script should do it:

#!/bin/bash

for pid in $(ps -ef | awk '{print $2}'); do
    if [ -f /proc/$pid/smaps ]; then
        echo "* Mem usage for PID $pid"     
        rss=$(awk 'BEGIN {i=0} /^Rss/ {i = i + $2} END {print i}' /proc/$pid/smaps)
        pss=$(awk 'BEGIN {i=0} /^Pss/ {i = i + $2 + 0.5} END {print i}' /proc/$pid/smaps)
        sc=$(awk 'BEGIN {i=0} /^Shared_Clean/ {i = i + $2} END {print i}' /proc/$pid/smaps)            
        sd=$(awk 'BEGIN {i=0} /^Shared_Dirty/ {i = i + $2} END {print i}' /proc/$pid/smaps)
        pc=$(awk 'BEGIN {i=0} /^Private_Clean/ {i = i + $2} END {print i}' /proc/$pid/smaps)
        pd=$(awk 'BEGIN {i=0} /^Private_Dirty/ {i = i + $2} END {print i}' /proc/$pid/smaps)
        echo "-- Rss: $rss kB" 
        echo "-- Pss: $pss kB"
        echo "Shared Clean $sc kB"
        echo "Shared Dirty $sd kB"
        echo "Private $(($pd + $pc)) kB"
    fi
done
share|improve this answer
    
Outputs: line 16: + : syntax error: operand expected (error token is "+ ") which is the line that contains: echo "Private $(($pd + $pc)) kB", I'm running on CentOS 7. – Basil A Jan 10 at 17:19
1  
@BasilA - Thanks, fixed that – evsmith Jan 10 at 23:17

List processes by mem usage

command : ps -e -orss=,args= | sort -b -k1,1n | pr -TW$COLUMNS\

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .