total percentage of memory use for all processes with a given name

ps -eo pmem,comm | grep chrome | cut -d " " -f 2 | paste -sd+ | bc

0
By: peter1337
2016-01-24 20:45:04

1 Alternatives + Submit Alt

  • This command will add up RAM usage of all processes whose name contains "java" and output the sum of percentages in HRF. Also, unlike the original #15430, it wont fail on processes with a usage of >9.9%. Pleases note that this command wont work reliably in use cases where a significant portion of processes involved are using less than 0.1% of RAM, because they will be counted as "0", even though a great number of them could add up to significant amounts. Show Sample Output


    5
    ps -eo pmem,comm | grep java | awk '{sum+=$1} END {print sum " % of RAM"}'
    bugmenot · 2016-02-10 09:00:56 0

What Others Think

What about specifying the process to grep from standard input? grep "$1" should work, but one of the other piped command doesn't like it. Also, I've added the following after the bc pipe to give you a percent sign and some explanatory text: sed 's/$/% of RAM used/'
sophiejane · 138 weeks and 5 days ago
This command sequence will fail once one of the processes listed occupies more than 9.9% of available ram because the formatting will cause the cut sequence to break. So, you better put aewk to the task: ps -eo pmem,comm | grep java | awk '{ sum+=$1} END {print sum}'
bugmenot · 136 weeks and 5 days ago
Or, including xiaozhengm's suggestion: ps -eo pmem,comm | grep java | awk '{ sum+=$1} END {print sum " of RAM used"}'
bugmenot · 136 weeks and 5 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: