Get the highlights in your inbox every week.
Get memory use statistics with this Linux command-line tool | Opensource.com
Get memory use statistics with this Linux command-line tool
The smem command allows you to quickly view your web applications' memory use.
To interrogate memory use on Linux or macOS, people typically use top or htop. I'd love to see a single number: How much RAM did a process take. But statistics shown by these utilities can be hard to understand. With web browsers, it's even more complicated because they often run many separate processes. They all show up in top output as a long list, each with its own individual metrics.
Enter smem command
Luckily there is smem, another command-line utility for viewing memory use statistics. Install it with your package manager of choice, for example:
sudo apt install smem
To get total memory use by Firefox, do:
smem -c pss -P firefox -k -t | tail -n 1
What happens here?
-cswitch specifies columns to show. I'm only interested in the pss column, which shows memory allocated by a process.
-Pswitch filters processes to include only those with firefox in the name
-kswitch tells to show memory use in mega/gigabytes instead of plain bytes
-tswitch displays the totals
tail -n 1filter outputs only the last line, just where the totals are
The output is as simple as it gets:
$ smem -t -k -c pss -P firefox | tail -n 1
Straight to the point! And, after another busy day of work, with over fifty opened tabs, Firefox still uses only 5 GB. Take that, Google Chrome ;-)
Even easier with a script
For convenience, create a little script named
memory-use, which takes the process name as a parameter. I keep all my scripts in
echo 'smem -c pss -P "$1" -k -t | tail -n 1' > ~/bin/memory-use && chmod +x ~/bin/memory-use
Now I can measure memory use of any application as easy as:
And there is even more!
The utility can do much more than show the total memory use. It can even generate graphic output.
smem --pie name -c pss
Shows something like this:
For more details, I recommend looking into smem man pages.
You can find another great tutorial at https://linoxide.com/memory-usage-reporting-smem/.
This article originally appeared on the author's blog and is republished with permission.