12

Is there a way to group similar processes when using tools like top/htop? Sometimes I just want to know what's eating my memory and some programs (browsers mostly) are using multiple processes, which makes it hard to read how much RAM they really use.

So far I came up only with something like that:

ps ax -o pmem,cmd | grep opera | grep -oE '^[ ]*[0-9.]+' | paste -sd+ - | bc

4 Answers 4

11
+50

You can use ps -C to only display process information for a particular command name.

e.g.

ps -C opera

You can then use other ps options to extract just the data you are looking for. In particular, h or --no-headers to suppress the column headers, and -o pmem to show the percentage of memory used by the process.

ps -C opera --no-headers -o pmem

That will give you a bunch of memory-usage percentages, one per line.

There are numerous methods for summing data like that, one of the methods I use frequently is to pipe it into xargs to convert it into one line with elements delimited by spaces, then into sed to convert spaces to + symbols, and then into bc to perform the calculation. Your method of piping into paste -sd+ works as well or arguably better than | xargs | sed.

Putting that all together, you get:

ps -C opera --no-headers -o pmem | xargs | sed -e 's/ /+/g' | bc

or

ps -C opera --no-headers -o pmem | paste -sd+ | bc

In other words, you can use ps -C instead of multiple greps if you just want data about one particular running program.

NOTE: You can use multiple -C options on the same command line if you want info about more than one program at a time. e.g.

ps -C iceweasel -C chromium -C opera
3
  • 1
    Yay, first person that actually understood/read my question!
    – korda
    Oct 14, 2015 at 8:24
  • Only problem I see with this approach is that some programs have same command and yet I wouldn't want to group them (for example java... and since I'm java developer I ussually have at least a few java programs running). I guess there is no perfect solution...
    – korda
    Oct 14, 2015 at 8:34
  • there's always ps h -C java -o pmem,args | awk '/foo/ {print $1}' | ... e.g. to look for a java program called foo.
    – cas
    Oct 14, 2015 at 8:50
1

From inside top you can use the following:

Press SHIFT+f
Press the Letter corresponding to %MEM
Press ENTER

You might also try:

ps -eo pmem,pcpu,vsize,pid,cmd | sort -k 1 -nr | head -5

This command will give the top 5 processes by memory usage.

I hope you find this info helpful!

0

You can make a shell script

#!/bin/bash
Total=`cat /proc/meminfo | head -n1 | awk '{print $2}'`
max_usage=`ps ax -o pmem,fname | sort -r | head -n2 | tail -n1 | awk '{print $1}'`
name=`ps ax -o pmem,fname | sort -r | head -n2 | tail -n1 | awk '{print $NF}'`
echo "$Total $max_usage $name"
echo "($max_usage*$Total)/(100*1024)" | bc

This will give the highest mem usage Application.

6
  • I've tried to get this working and it didn't give me interestening results. For example it only shows one program, and it isn't the one taking up the most of the memory
    – korda
    Oct 9, 2015 at 8:59
  • Is it? Well you want some thing like top?? if yes then you can modify the script with delay of 2 or 1 sec and run the same thing in some while or for. For me it shows the highest Memory usage Application at the time i run this Script. what kind of stuff you are trying, just run : ps af -o pmem,cmd | sort -r It will give you the highest mem usage application
    – RahulAN
    Oct 9, 2015 at 9:28
  • It's not working for me. I guess the reason is that sort includes leading white space
    – korda
    Oct 9, 2015 at 10:52
  • You mean you are not getting correct name of process??
    – RahulAN
    Oct 9, 2015 at 12:33
  • Yes, beacuse ps add spaces to indent stuff. When one process has 2 digit usage one digit usage values will be indented and will start with leading space. I guess adding something like sed -e 's/^[[:space:]]*//' should fix it. Also does your script adds up memory usage of processes with same command?
    – korda
    Oct 9, 2015 at 12:39
0

This can be simplified with termsql.

ps -C chromium-browser -o pmem | termsql -1 "SELECT SUM([%MEM]) FROM tbl"

Note, that currently it's not upload to PyPI, and also can only be installed system-wide (setup.py is a little broken), like sudo pip install https://github.com/tobimensch/termsql/archive/master.zip.

termsql is available on PyPI, and can be installed e.g. with pipx install termsql.

You must log in to answer this question.

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