用 smem 命令获取内存使用统计信息
smem 内存

> 
> smem 命令允许你快速查看你的网页应用的内存使用情况。
> 
> 
> 


![](/data/attachment/album/202110/25/101843emjjkmvk88gvyqgv.jpg "Programming at a browser, orange hands")


在我的编程工作中，我经常需要了解网页应用的内存使用情况。在深入研究细节和浏览器剖析工具之前，一个粗略的估计通常就足够了。


为了了解 Linux 或 macOS 上的内存使用情况，人们通常使用 [top](https://linux.die.net/man/1/top) 或 [htop](https://linux.die.net/man/1/htop)。我很想看到一个单一的数字：一个进程占用了多少内存。但这些工具所显示的统计数据可能很难理解。对于网页浏览器来说，它甚至更加复杂，因为它们经常运行许多独立的进程。它们在 `top` 输出中显示为一个长长的列表，每一个都有自己的单独指标。


![Memory usage using htop](/data/attachment/album/202110/25/101853izcyy0bttz1ltbx2.png "Memory usage using htop")


### smem 命令


幸运的是有 [smem](https://linux.die.net/man/8/smem)，另一个用于查看内存使用统计的命令行工具。用你选择的包管理器安装它，例如：



```
sudo apt install smem

```

要获得 [Firefox](https://www.mozilla.org/en-US/firefox/) 的总内存使用量，请执行：



```
smem -c pss -P firefox -k -t | tail -n 1

```

这些开关做了什么？


* `-c` 开关指定要显示的列。我只对 `pss` 列感兴趣，它显示一个进程分配的内存。
* `-P` 开关过滤进程，只包括那些名字里有 `firefox` 的进程。
* `-k` 开关显示以 MB/GB 为单位的内存使用情况，而不是单纯的字节数。
* `-t` 开关显示总数。
* `tail -n 1` 过滤器只输出最后一行，也就是总数的地方。


输出是非常简单的：



```
$ smem -t -k -c pss -P firefox | tail -n 1
4.9G

```

开门见山！而且，经过又一天忙碌的工作，打开了 50 多个选项卡，Firefox 仍然只使用 5 GB。看看吧，Google Chrome。


#### 用一个脚本更容易


为了方便起见，我创建一个名为 `memory-use` 的小脚本，它将进程名称作为参数。我把所有的脚本都放在 `~/bin` 里，所以：



```
echo 'smem -c pss -P "$1" -k -t | tail -n 1' > ~/bin/memory-use && chmod +x ~/bin/memory-use

```

现在我可以很容易地测量任何应用的内存使用：



```
memory-use firefox
memory-use chrome
memory-use slack

```

#### 还有更多！


该工具不仅可以显示总的内存使用情况。它甚至可以生成图形输出。


比如：



```
smem --pie name -c pss

```

显示类似这样的内容：


![Pie chart output from smem](/data/attachment/album/202110/25/101854lvgfcmc3ikkeb6mh.png "Pie chart output from smem")


关于更多的细节，我建议查看 [smem 手册页](https://linux.die.net/man/8/smem)。


你可以在 <https://linoxide.com/memory-usage-reporting-smem/> 上找到另一个很棒的教程。


希望你喜欢!


本文最初发表在[作者的博客](https://letsdebug.it/post/26-measure-application-memory-use-on-linux/)上，并经许可转载。




---


via: <https://opensource.com/article/21/10/memory-stats-linux-smem>


作者：[Tomasz Waraksa](https://opensource.com/users/tomasz) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
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, people typically use [top](https://linux.die.net/man/1/top) or [htop](https://linux.die.net/man/1/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.

![Memory usage using htop Memory usage using htop](https://opensource.com/sites/default/files/uploads/1_htop.png)

(Tomasz Waraksa, CC BY-SA 4.0)

# Enter smem command

Luckily there is [smem](https://linux.die.net/man/8/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](https://www.mozilla.org/en-US/firefox/), 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:

`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 show the total memory use. It can even generate graphic output.

For example:

`smem --pie name -c pss`


Shows something like this:

![Pie chart output from htop Pie chart output from smem](https://opensource.com/sites/default/files/uploads/2_smem-pie-chart.png)

(Tomasz Waraksa, CC BY-SA 4.0)

For more details, I recommend looking into [smem man pages](https://linux.die.net/man/8/smem).

You can find another great tutorial at [https://linoxide.com/memory-usage-reporting-smem/](https://linoxide.com/memory-usage-reporting-smem/).

Enjoy!

*This article originally appeared on the author's blog and is republished with permission.*

## 1 Comment