Posted on March 14, 2015
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:
- Get the percent of memory used (pmem) and the command name (comm) of every process.
- Select only chromium results.
- Break each result into chucks and then select the chunk containing the percent value.
- Join the results together with the addition symbol (+).
- Add them up.
- 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:
- Get the amount of memory in use (RSS) and the command name of every process.
- Select only chromium results.
- Remove the word “chromium”, so only the rss value is left.
- Join the results together with the addition symbol (+).
- Add them up.
- Use awk to convert the result from KiB to GiB by dividing by 1024²
- Also use awk to print the result to a maximum of 2 decimal places and add the text “GiB” so it looks nicer.