How to quickly view applications' memory use with smem
Contents
Introduction
In my programming work I often need to know memory use by applications. Rough estimate is usually enough, before getting down to details and browser profiling tools.
To interrogate memory use on Linux or MacOS we 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 difficult, because they often run many separate processes. The same is true for Electron-based applications such as Visual Studio Code. They will 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 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?
-c
switch specifies columns to show. We’re only interested in pss column which shows memory allocated by a process.-P
switch filters processes to include only those with firefox in the name-k
switch tells to show memory use in mega/gigabytes, instead of plain bytes-t
switch displays the totalstail -n 1
filter 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
4.9G
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 process name as parameter. I keep all my scripts in ~/bin
, so:
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:
memory-use firefox
memory-use chrome
memory-use slack
…and there is even more!
The utility can do much more than just show the total memory use. It can even generate graphic output. Try for example:
smem --pie name -c pss
to see something like this:
For more details I recommend looking into smem man pages.
Another great tutorial can be found at https://linoxide.com/memory-usage-reporting-smem/.
Enjoy!
Author Tomasz Waraksa
LastMod 2021-10-05
© Tomasz Waraksa, letsdebug.it
Disclaimer
This article is not promoting any of the described products, services or vendors. We don't have any commercial interest nor associations with them. We're not trying to suggest that these products or services are best for you, nor promising that your experience will be the same.
In no event will we be liable for any loss or damage including without limitation, indirect or consequential loss or damage, or any loss or damage whatsoever arising from loss of data or profits arising out of, or in connection with, the use of this website and any information presented on it.
Through this website you are able to link to other websites which are not under our control. We have no control over the nature, content and availability of those sites. The inclusion of any links does not necessarily imply a recommendation or endorse the views expressed within them.
Software License
Permission is hereby granted, free of charge, to any person obtaining a copy of software published on this website and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions, unless stated explicitly otherwise:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
The software is provided “as is”, without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the software.