使用 top 命令了解 Fedora 的内存使用情况
top 内存
![](/data/attachment/album/201809/26/101505mz8fqz2wfv2wzfvk.jpg)


如果你使用过 `top` 命令来查看 Fedora 系统中的内存使用情况，你可能会惊讶，看起来消耗的数量比系统可用的内存更多。下面会详细介绍内存使用情况以及如何理解这些数据。


### 内存实际使用情况


操作系统对内存的使用方式并不是太通俗易懂。事实上，其背后有很多不为人知的巧妙技术在发挥着作用。通过这些方式，可以在无需用户干预的情况下，让操作系统更有效地使用内存。


大多数应用程序都不是系统自带的，但每个应用程序都依赖于安装在系统中的库中的一些函数集。在 Fedora 中，RPM 包管理系统能够确保在安装应用程序时也会安装所依赖的库。


当应用程序运行时，操作系统并不需要将它要用到的所有信息都加载到物理内存中。而是会为存放代码的存储空间构建一个映射，称为虚拟内存。操作系统只把需要的部分加载到内存中，当某一个部分不再需要后，这一部分内存就会被释放掉。


这意味着应用程序可以映射大量的虚拟内存，而使用较少的系统物理内存。特殊情况下，映射的虚拟内存甚至可以比系统实际可用的物理内存更多！而且在操作系统中这种情况也并不少见。


另外，不同的应用程序可能会对同一个库都有依赖。Fedora 中的 Linux 内核通常会在各个应用程序之间共享内存，而不需要为不同应用分别加载同一个库的多个副本。类似地，对于同一个应用程序的不同实例也是采用这种方式共享内存。


如果不首先了解这些细节，`top` 命令显示的数据可能会让人摸不着头脑。下面就举例说明如何正确查看内存使用量。


### 使用 `top` 命令查看内存使用量


如果你还没有使用过 `top` 命令，可以打开终端直接执行查看。使用 `Shift + M` 可以按照内存使用量来进行排序。下图是在 Fedora Workstation 中执行的结果，在你的机器上显示的结果可能会略有不同：


![](/data/attachment/album/201809/26/101505xhxpby0whgpvcvig.png)


主要通过以下三列来查看内存使用情况：`VIRT`、`RES` 和 `SHR`。目前以 KB 为单位显示相关数值。


`VIRT` 列代表该进程映射的<ruby> 虚拟 <rt>  virtual </rt></ruby>内存。如上所述，虚拟内存不是实际消耗的物理内存。例如， GNOME Shell 进程 `gnome-shell` 实际上没有消耗超过 3.1 GB 的物理内存，但它对很多更低或更高级的库都有依赖，系统必须对每个库都进行映射，以确保在有需要时可以加载这些库。


`RES` 列代表应用程序消耗了多少实际（<ruby> 驻留 <rt>  resident </rt></ruby>）内存。对于 GNOME Shell 大约是 180788 KB。例子中的系统拥有大约 7704 MB 的物理内存，因此内存使用率显示为 2.3％。


但根据 `SHR` 列显示，其中至少有 88212 KB 是<ruby> 共享 <rt>  shared </rt></ruby>内存，这部分内存可能是其它应用程序也在使用的库函数。这意味着 GNOME Shell 本身大约有 92 MB 内存不与其他进程共享。需要注意的是，上述例子中的其它程序也共享了很多内存。在某些应用程序中，共享内存在内存使用量中会占很大的比例。


值得一提的是，有时进程之间通过内存通信，这些内存也是共享的，但 `top` 这样的工具却不一定能检测到，所以以上的说明也不一定准确。


### 关于交换分区


系统还可以通过交换分区来存储数据（例如硬盘），但读写的速度相对较慢。当物理内存渐渐用满，操作系统就会查找内存中暂时不会使用的部分，将其写出到交换区域等待需要的时候使用。


因此，如果交换内存的使用量一直偏高，表明系统的物理内存已经供不应求了。有时候一个不正常的应用也有可能导致出现这种情况，但如果这种现象经常出现，就需要考虑提升物理内存或者限制某些程序的运行了。


感谢 [Stig Nygaard](https://www.flickr.com/photos/stignygaard/) 在 [Flickr](https://www.flickr.com/photos/stignygaard/3138001676/) 上提供的图片（CC BY 2.0）。




---


via: <https://fedoramagazine.org/understand-fedora-memory-usage-top/>


作者：[Paul W. Frields](https://fedoramagazine.org/author/pfrields/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[HankChow](https://github.com/HankChow) 校对：[wxy](https://github.com/wxy)


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


OK
Have you used the *top* utility in a terminal to see memory usage on your Fedora system? If so, you might be surprised to see some of the numbers there. It might look like a lot more memory is consumed than your system has available. This article will explain a little more about memory usage, and how to read these numbers.


## Memory usage in real terms

The way the operating system (OS) uses memory may not be self-evident. In fact, some ingenious, behind-the-scenes techniques are at play. They help your OS use memory more efficiently, without involving you.

Most applications are not self contained. Instead, each relies on sets of functions collected in libraries. These libraries are also installed on the system. In Fedora, the RPM packaging system ensures that when you install an app, any libraries on which it relies are installed, too.

When an app runs, the OS doesn’t necessarily load all the information it uses into real memory. Instead, it builds a map to the storage where that code is stored, called *virtual memory*. The OS then loads only the parts it needs. When it no longer needs portions of memory, it might release or swap them out as appropriate.

This means an app can map a very large amount of virtual memory, while using less real memory on the system at one time. It might even map more RAM than the system has available! In fact, across a whole OS that’s often the case.

In addition, related applications may rely on the same libraries. The Linux kernel in your Fedora system often shares memory between applications. It doesn’t need to load multiple copies of the same library for related apps. This works similarly for separate instances of the same app, too.

Without understanding these details, the output of the *top* application can be confusing. The following example will clarify this view into memory usage.

## Viewing memory usage in *top*

If you haven’t tried yet, open a terminal and run the *top* command to see some output. Hit **Shift+M** to see the list sorted by memory usage. Your display may look slightly different than this example from a running Fedora Workstation:

There are three columns showing memory usage to examine: *VIRT, RES, *and *SHR*. The measurements are currently shown in kilobytes (KB).

The VIRT column is the virtual memory mapped for this process. Recall from the earlier description that virtual memory is not actual RAM consumed. For example, the GNOME Shell process *gnome-shell* is not actually consuming over 3.1 gigabytes of actual RAM. However, it’s built on a number of lower and higher level libraries. The system must map each of those to ensure they can be loaded when necessary.

The RES column shows you how much actual *(resident)* memory is consumed by the app. In the case of GNOME Shell, that’s about 180788 KB. The example system has roughly 7704 MB of physical memory, which is why the memory usage shows up as 2.3%.

However, of that number, at least 88212 KB is *shared* memory, shown in the SHR column. This memory might be, for example, library functions that other apps also use. This means the GNOME Shell is using about 92 MB on its own not shared with other processes. Notice that other apps in the example share an even higher percentage of their resident memory. In some apps, the shared portion is the vast majority of the memory usage.

There is a wrinkle here, which is that sometimes processes communicate with each other via memory. That memory is also shared, but can’t necessarily be detected by a utility like *top*. So yes — even the above clarifications still have some uncertainty!

## A note about swap

Your system has another facility it uses to store information, which is *swap*. Typically this is an area of slower storage (like a hard disk). If the physical memory on the system fills up as needs increase, the OS looks for portions of memory that haven’t been needed in a while. It writes them out to the swap area, where they sit until needed later.

Therefore, prolonged, high swap usage usually means a system is suffering from too little memory for its demands. Sometimes an errant application may be at fault. Or, if you see this often on your system, consider upgrading your machine’s memory, or restricting what you run.

*Photo courtesy of Stig Nygaard, via Flickr (CC BY 2.0).*

## Leslie Satenstein

Thank you for this very useful expository of Linux hosting Gnome and the impact to memory consumption.

Does Linux provide historical measures or stats about the I/O activity for swapping?

## Yury D

No, it doesn’t(apart from wait indicator—0.2 wa in the screenshot), but atop does.

## Diego Roberto dos Santos

Hi Leslie,

In short you need to enable sysstat to collect system activity information, then these data will be stored in disk under the “/var/log/sa” directory and later you will be able to review historical by querying this files using the sar command. For further examples, please see [1][2].

[1] https://github.com/sysstat/sysstat

[2] https://www.thegeekstuff.com/2011/03/sar-examples/

## Leslie Satenstein

Hola Diego,

Gracias. I do know a little more Spanish, as my wife taught me to listen and learn. It is her mother tongue.

I am definitely going to refer to and setup some testing with sysstat. My intention is to use sysstat for a month after a new installation of a Fedora Releases

## Martin

There is iotop for I/O and iftop for network traffic monitoring. Historical data is not retained by my knowledge.

## Kerel

Paul, what’s an easy way to see if your system relies a lot on swap memory?

## Yury D

I usually look at the how much swap space is used, activity of the kswapd process, and how much exercise the swap partitions get. atop is much better at display that last than top, in my opinion.

## alex

What about processes like apache/httpd that show up in

with all their forks , and every one of them are displayed as using memory/cpu.

How do you interpret that ?

## Einer

Leslie Satenstein,

Yes, you can do this a multitude of ways. SAR is the most commonly used.

Kerel,

Depends ….. but SAR can usually give you some idea or you’ll be looking at top for quite some time to catch/estimate the swap area usage.

Alex,

On multithreaded apps, like httpd, the memory reported by top for the thread is the same as the parent … all threads of the parent process share the parents space … so, the parent process is the one you would usually look at and use as a guide for usage. That having been said, there are cases when you will see a particular thread using more memory thant the others in the same process ……

Paul,

SWAP is not always a good indicator of RAM exhaustion. Remember that swap is also used for COW pages (Copy On Write) …… a crude example would be things like DB doing interprocess communications … even vi does this. The typical indication of RAM exhaustion is when you see a lot of I/O wait and so/bo when running vmstat 5 ….

top is a great utility to get a quick realtime look at what is going on but, you also need other tools to get the full picture 🙂

## Leslie Satenstein

Einer,

Thank you all for the feedback. Putting your suggestions together with Yury and with Diego,

I will workout a measuring plan.

## svsv sarma

Thank you. Throws a light on TOP utility and memory usage. Now I am happy to see that my note book has memory 4GB enough and to spare and SWAP is used rarely. This I can see with System Monitor/Resources also. However TOP takes a special study.

## Gudrun Khom

Werde ich da grad verapplet — Ich will dass das Programm funktioniert und mich auf Deutsch unterhalten …

## Paul W. Frields

If you want Fedora to work in German, use the

Region and Languagesettings to add support for German language. Note that when you installed, you were asked to select the language as the first step. For further help, please consult the help page here, since the Magazine is not a support forum: https://fedoraproject.org/wiki/Communicating_and_getting_help## alex

Thank you

Einerfor the clarification , i did not know that 🙂## John Lukas

sar command and sar graph is a nice tool to gather memory info.

$ sudo sar -r 1 3

Refer : https://linoxide.com/linux-command/system-performance-monitoring-sar-command/

## Dan Marinescu

generally, when one presents a set of concepts, does not use undefined terms. apparently, no such case here. you are using resident memory without explaining what it is.