Get memory use statistics with this Linux command-line tool
======
The smem command allows you to quickly view your web applications'
memory use.
![Programming at a browser, orange hands][1]
In my programming work, I often need to know the memory used by web applications. A rough estimate is usually enough before getting down to details and browser profiling tools.
To interrogate memory use on Linux or macOS, peopletypically use[top][2]or[htop][3]. 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.
![Memory usage using htop][4]
(Tomasz Waraksa, [CC BY-SA 4.0][5])
### Enter smem command
Luckily there is[smem][6], 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][7], do:
```
`smem -c pss -P firefox -k -t | tail -n 1`
```
What happens here?
*`-c` switch specifies columns to show. I'm only interested in_the 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 totals
*`tail -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 the process name as a parameter. I keep all my scripts in `~/bin`, so:
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/programming_code_keyboard_orange_hands.png?itok=G6tJ_64Y (Programming at a browser, orange hands)
[2]: https://linux.die.net/man/1/top
[3]: https://linux.die.net/man/1/htop
[4]: https://opensource.com/sites/default/files/uploads/1_htop.png (Memory usage using htop)