Colin Robinson

Just another guy on the internet

Use Bash to get the memory usage of applications like Chromium

It’s not always easy to tell how much memory Chromium/Chrome is using, because of the different threads it starts. Here are a couple snippets with explanations to get you started.

Percent of memory use

Code:

ps -eo pmem,comm | grep chromium | cut -d " " -f 2 | paste -sd+ | bc | awk '{ print $1 "%" }'

Output:

17.5%

Explanation:

  1. Get the percent of memory used (pmem) and the command name (comm) of every process.
  2. Select only chromium results.
  3. Break each result into chucks and then select the chunk containing the percent value.
  4. Join the results together with the addition symbol (+).
  5. Add them up.
  6. Print the value with a percent symbol so it looks nicer.

Real memory use

Code:

ps -eo rss,comm | \grep chromium | sed 's/chromium//' | paste -sd+ | bc | awk '{printf( "%0.2f GiB\n", $1/1024^2 )}'

Output:

1.45 GiB

Explanation:

  1. Get the amount of memory in use (RSS) and the command name of every process.
  2. Select only chromium results.
  3. Remove the word “chromium”, so only the rss value is left.
  4. Join the results together with the addition symbol (+).
  5. Add them up.
  6. Use awk to convert the result from KiB to GiB by dividing by 1024²
  7. Also use awk to print the result to a maximum of 2 decimal places and add the text “GiB” so it looks nicer.

Leave a Reply

Your email address will not be published.