Home » Questions » Computers [ Ask a new question ]

How can I display the memory usage of each process if I do a 'ps -ef'?

How can I display the memory usage of each process if I do a 'ps -ef'?

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.

Asked by: Guest | Views: 425
Total answers/comments: 5
Guest [Entry]

"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"
Guest [Entry]

"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."
Guest [Entry]

"I found that many times same process has fork sub process and you are interested in total memory used by the process. e.g. google chrome starts multiple process and you wish to know total memory taken by chrome. I found below one line command very useful

echo ""%CPU %MEM MEM PROCESS""
ps aux | awk '{mem[$11]+=int($6/1024)}; {cpuper[$11]+=$3};{memper[$11]+=$4}; END {for (i in mem) {print cpuper[i]""% "",memper[i]""% "",mem[i]"" MB "",i}}' | sort -k3nr | head -n 5

It takes care of lots of things, like showing memory in MB, sorting on memory and mainly grouping by command. It is also grouping %CPU and %memory. It is showing data in user friendly format.

This command was inspired from this answer and it helped me lot to get idea on which process is taking up my resources."
Guest [Entry]

"List processes by mem usage

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

"Update:

The following one-liner also provides information on total memory consumption by the current user.

echo ""------------------------------------"" && mem=0 && while read -r rss comm ; do mbs=$((rss/1024)); mem=$((mbs + mem)); echo $mbs""MB - $comm""; done <<< ""$(ps -u $USER -wo rss=,comm= --sort -rss)"" && echo ""------------------------------------"" && echo $mem""MB: Memory used by user '$USER'""

I have just listed all user processes sorted by the highest memory usage in MB like this:

ps -u $USER -wo rss=,comm= --sort -rss | while read -r rss comm ; do echo $((rss/1024))""MB -"" $comm; done

You can use command= instead of comm= to show the full path process.

The = just removes the column title."