Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

In linux, how can I display memory usage of each process if i do a 'ps -ef'? I would like to 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 {my process name}'.

Thank you.

share|improve this question

4 Answers 4

up vote 4 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

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 at 22:45

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

you can use

top -n 1
share|improve this answer

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.