mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-03-21 02:10:11 +08:00
Merge pull request #9636 from MjSeven/master
20180615 5 Commands for Checking Memory Usage in Linux.md 翻译完毕
This commit is contained in:
commit
a2f79ee34e
@ -1,198 +0,0 @@
|
||||
Translating by MjSeven
|
||||
|
||||
|
||||
5 Commands for Checking Memory Usage in Linux
|
||||
======
|
||||
|
||||

|
||||
The Linux operating system includes a plethora of tools, all of which are ready to help you administer your systems. From simple file and directory tools to very complex security commands, there’s not much you can’t do on Linux. And, although regular desktop users may not need to become familiar with these tools at the command line, they’re mandatory for Linux admins. Why? First, you will have to work with a GUI-less Linux server at some point. Second, command-line tools often offer far more power and flexibility than their GUI alternative.
|
||||
|
||||
Determining memory usage is a skill you might need should a particular app go rogue and commandeer system memory. When that happens, it’s handy to know you have a variety of tools available to help you troubleshoot. Or, maybe you need to gather information about a Linux swap partition or detailed information about your installed RAM? There are commands for that as well. Let’s dig into the various Linux command-line tools to help you check into system memory usage. These tools aren’t terribly hard to use, and in this article, I’ll show you five different ways to approach the problem.
|
||||
|
||||
I’ll be demonstrating on the [Ubuntu Server 18.04 platform][1]. You should, however, find all of these commands available on your distribution of choice. Even better, you shouldn’t need to install a single thing (as most of these tools are included).
|
||||
|
||||
With that said, let’s get to work.
|
||||
|
||||
### top
|
||||
|
||||
I want to start out with the most obvious tool. The top command provides a dynamic, real-time view of a running system. Included in that system summary is the ability to check memory usage on a per-process basis. That’s very important, as you could easily have multiple iterations of the same command consuming different amounts of memory. Although you won’t find this on a headless server, say you’ve opened Chrome and noticed your system slowing down. Issue the top command to see that Chrome has numerous processes running (one per tab - Figure 1).
|
||||
|
||||
![top][3]
|
||||
|
||||
Figure 1: Multiple instances of Chrome appearing in the top command.
|
||||
|
||||
[Used with permission][4]
|
||||
|
||||
Chrome isn’t the only app to show multiple processes. You see the Firefox entry in Figure 1? That’s the primary process for Firefox, whereas the Web Content processes are the open tabs. At the top of the output, you’ll see the system statistics. On my machine (a [System76 Leopard Extreme][5]), I have a total of 16GB of RAM available, of which just over 10GB is in use. You can then comb through the list and see what percentage of memory each process is using.
|
||||
|
||||
One of the things top is very good for is discovering Process ID (PID) numbers of services that might have gotten out of hand. With those PIDs, you can then set about to troubleshoot (or kill) the offending tasks.
|
||||
|
||||
If you want to make top a bit more memory-friendly, issue the command top -o %MEM, which will cause top to sort all processes by memory used (Figure 2).
|
||||
|
||||
![top][7]
|
||||
|
||||
Figure 2: Sorting process by memory used in top.
|
||||
|
||||
[Used with permission][4]
|
||||
|
||||
The top command also gives you a real-time update on how much of your swap space is being used.
|
||||
|
||||
### free
|
||||
|
||||
Sometimes, however, top can be a bit much for your needs. You may only need to see the amount of free and used memory on your system. For that, there is the free command. The free command displays:
|
||||
|
||||
* Total amount of free and used physical memory
|
||||
|
||||
* Total amount of swap memory in the system
|
||||
|
||||
* Buffers and caches used by the kernel
|
||||
|
||||
|
||||
|
||||
|
||||
From your terminal window, issue the command free. The output of this command is not in real time. Instead, what you’ll get is an instant snapshot of the free and used memory in that moment (Figure 3).
|
||||
|
||||
![free][9]
|
||||
|
||||
Figure 3: The output of the free command is simple and clear.
|
||||
|
||||
[Used with permission][4]
|
||||
|
||||
You can, of course, make free a bit more user-friendly by adding the -m option, like so: free -m. This will report the memory usage in MB (Figure 4).
|
||||
|
||||
![free][11]
|
||||
|
||||
Figure 4: The output of the free command in a more human-readable form.
|
||||
|
||||
[Used with permission][4]
|
||||
|
||||
Of course, if your system is even remotely modern, you’ll want to use the -g option (gigabytes), as in free -g.
|
||||
|
||||
If you need memory totals, you can add the t option like so: free -mt. This will simply total the amount of memory in columns (Figure 5).
|
||||
|
||||
![total][13]
|
||||
|
||||
Figure 5: Having free total your memory columns for you.
|
||||
|
||||
[Used with permission][4]
|
||||
|
||||
### vmstat
|
||||
|
||||
Another very handy tool to have at your disposal is vmstat. This particular command is a one-trick pony that reports virtual memory statistics. The vmstat command will report stats on:
|
||||
|
||||
* Processes
|
||||
|
||||
* Memory
|
||||
|
||||
* Paging
|
||||
|
||||
* Block IO
|
||||
|
||||
* Traps
|
||||
|
||||
* Disks
|
||||
|
||||
* CPU
|
||||
|
||||
|
||||
|
||||
|
||||
The best way to issue vmstat is by using the -s switch, like vmstat -s. This will report your stats in a single column (which is so much easier to read than the default report). The vmstat command will give you more information than you need (Figure 6), but more is always better (in such cases).
|
||||
|
||||
![vmstat][15]
|
||||
|
||||
Figure 6: Using the vmstat command to check memory usage.
|
||||
|
||||
[Used with permission][4]
|
||||
|
||||
### dmidecode
|
||||
|
||||
What if you want to find out detailed information about your installed system RAM? For that, you could use the dmidecode command. This particular tool is the DMI table decoder, which dumps a system’s DMI table contents into a human-readable format. If you’re unsure as to what the DMI table is, it’s a means to describe what a system is made of (as well as possible evolutions for a system).
|
||||
|
||||
To run the dmidecode command, you do need sudo privileges. So issue the command sudo dmidecode -t 17. The output of the command (Figure 7) can be lengthy, as it displays information for all memory-type devices. So if you don’t have the ability to scroll, you might want to send the output of that command to a file, like so: sudo dmidecode -t 17 > dmi_infoI, or pipe it to the less command, as in sudo dmidecode | less.
|
||||
|
||||
![dmidecode][17]
|
||||
|
||||
Figure 7: The output of the dmidecode command.
|
||||
|
||||
[Used with permission][4]
|
||||
|
||||
### /proc/meminfo
|
||||
|
||||
You might be asking yourself, “Where do these commands get this information from?”. In some cases, they get it from the /proc/meminfo file. Guess what? You can read that file directly with the command less /proc/meminfo. By using the less command, you can scroll up and down through that lengthy output to find exactly what you need (Figure 8).
|
||||
|
||||
![/proc/meminfo][19]
|
||||
|
||||
Figure 8: The output of the less /proc/meminfo command.
|
||||
|
||||
[Used with permission][4]
|
||||
|
||||
One thing you should know about /proc/meminfo: This is not a real file. Instead /pro/meminfo is a virtual file that contains real-time, dynamic information about the system. In particular, you’ll want to check the values for:
|
||||
|
||||
* MemTotal
|
||||
|
||||
* MemFree
|
||||
|
||||
* MemAvailable
|
||||
|
||||
* Buffers
|
||||
|
||||
* Cached
|
||||
|
||||
* SwapCached
|
||||
|
||||
* SwapTotal
|
||||
|
||||
* SwapFree
|
||||
|
||||
|
||||
|
||||
|
||||
If you want to get fancy with /proc/meminfo you can use it in conjunction with the egrep command like so: egrep --color 'Mem|Cache|Swap' /proc/meminfo. This will produce an easy to read listing of all entries that contain Mem, Cache, and Swap ... with a splash of color (Figure 9).
|
||||
|
||||
![/proc/meminfo][21]
|
||||
|
||||
Figure 9: Making /proc/meminfo easier to read.
|
||||
|
||||
[Used with permission][4]
|
||||
|
||||
### Keep learning
|
||||
|
||||
One of the first things you should do is read the manual pages for each of these commands (so man top, man free, man vmstat, man dmidecode). Starting with the man pages for commands is always a great way to learn so much more about how a tool works on Linux.
|
||||
|
||||
Learn more about Linux through the free ["Introduction to Linux" ][22]course from The Linux Foundation and edX.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.linux.com/learn/5-commands-checking-memory-usage-linux
|
||||
|
||||
作者:[Jack Wallen][a]
|
||||
选题:[lujun9972](https://github.com/lujun9972)
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:https://www.linux.com/users/jlwallen
|
||||
[1]:https://www.ubuntu.com/download/server
|
||||
[2]:/files/images/memory1jpg
|
||||
[3]:https://www.linux.com/sites/lcom/files/styles/rendered_file/public/memory_1.jpg?itok=fhhhUL_l (top)
|
||||
[4]:/licenses/category/used-permission
|
||||
[5]:https://system76.com/desktops/leopard
|
||||
[6]:/files/images/memory2jpg
|
||||
[7]:https://www.linux.com/sites/lcom/files/styles/rendered_file/public/memory_2.jpg?itok=zuVkQfvv (top)
|
||||
[8]:/files/images/memory3jpg
|
||||
[9]:https://www.linux.com/sites/lcom/files/styles/rendered_file/public/memory_3.jpg?itok=rvuQp3t0 (free)
|
||||
[10]:/files/images/memory4jpg
|
||||
[11]:https://www.linux.com/sites/lcom/files/styles/rendered_file/public/memory_4.jpg?itok=K_luLLPt (free)
|
||||
[12]:/files/images/memory5jpg
|
||||
[13]:https://www.linux.com/sites/lcom/files/styles/rendered_file/public/memory_5.jpg?itok=q50atcsX (total)
|
||||
[14]:/files/images/memory6jpg
|
||||
[15]:https://www.linux.com/sites/lcom/files/styles/rendered_file/public/memory_6.jpg?itok=bwFnUVmy (vmstat)
|
||||
[16]:/files/images/memory7jpg
|
||||
[17]:https://www.linux.com/sites/lcom/files/styles/rendered_file/public/memory_7.jpg?itok=UNHIT_P6 (dmidecode)
|
||||
[18]:/files/images/memory8jpg
|
||||
[19]:https://www.linux.com/sites/lcom/files/styles/rendered_file/public/memory_8.jpg?itok=t87jvmJJ (/proc/meminfo)
|
||||
[20]:/files/images/memory9jpg
|
||||
[21]:https://www.linux.com/sites/lcom/files/styles/rendered_file/public/memory_9.jpg?itok=t-iSMEKq (/proc/meminfo)
|
||||
[22]:https://training.linuxfoundation.org/linux-courses/system-administration-training/introduction-to-linux
|
@ -0,0 +1,190 @@
|
||||
用以检查 Linux 内存使用的 5 个命令
|
||||
======
|
||||
|
||||

|
||||
Linux 操作系统包含大量工具,所有这些工具都可以帮助你管理系统。从简单的文件和目录工具到非常复杂的安全命令,在 Linux 中没有多少是你做不了的。而且,尽管普通桌面用户可能不需要在命令行熟悉这些工具,但对于 Linux 管理员来说,它们是必需的。为什么?首先,你在某些时候不得不使用没有 GUI 的 Linux 服务器。其次,命令行工具通常比 GUI 替代工具提供更多的功能和灵活性。
|
||||
|
||||
确定内存使用情况是你可能需要的技能,尤其是特定应用程序变为流氓和占用系统内存时。当发生这种情况时,知道有多种工具可以帮助你进行故障排除十分方便的。或者,你可能需要收集有关 Linux 交换分区的信息,或者有关安装的 RAM 的详细信息?对于这些也有相应的命令。让我们深入了解各种 Linux 命令行工具,以帮助你检查系统内存使用情况。这些工具并不是非常难以使用,在本文中,我将向你展示五种不同的方法来解决这个问题。
|
||||
|
||||
我将在 [Ubuntu 18.04 服务器平台][1]上进行演示,但是你应该在你选择的发行版中找到对应的所有命令。更妙的是,你不需要安装任何东西(因为大多数这些工具都包含 Linux 系统中)。
|
||||
|
||||
话虽如此,让我们开始工作吧。
|
||||
|
||||
### top
|
||||
|
||||
我想从最明显的工具开始。top 命令提供正在运行的系统的动态实时视图,它检查每个进程的内存使用情况。这非常重要,因为你可以轻松地对同一命令的多次迭代消耗不同的内存量。虽然你无法在没有外设的服务器上找到它,但是你已经注意到打开 Chrome 使你的系统速度变慢了。发出 top 命令以查看 Chrome 有多个进程在运行(每个选项卡一个 - 图 1)。
|
||||
|
||||
![top][3]
|
||||
|
||||
图1:top 命令中出现多个 Chrome 进程。
|
||||
|
||||
(to 校正者:不知道这句话什么意思,难道是是否允许转载的?)
|
||||
[Used with permission][4]
|
||||
|
||||
Chrome 并不是唯一显示多个进程的应用。你看到图 1 中的 Firefox 了吗?那是 Firefox 的主进程,而 Web Content 进程是其打开的选项卡。在输出的顶部,你将看到系统统计信息。在我的机器上([System76 Leopard Extreme][5]),我总共有 16GB 可用 RAM,其中只有超过 10GB 的 RAM 正在使用中。然后,你可以整理列表,查看每个进程使用的内存百分比。
|
||||
|
||||
top 最好的事情之一就是发现可能已经失控的进程 ID(PID)服务数量。有了这些 PID,你可以对有问题的任务进行故障排除(或 kill)。
|
||||
|
||||
如果你想让 top 显示更友好的内存信息,使用命令 top -o%MEM,这会使 top 按进程所用内存对所有进程进行排序(图 2)。
|
||||
|
||||
|
||||
![top][7]
|
||||
|
||||
图 2:在 top 命令中按使用内存对进程排序
|
||||
|
||||
[Used with permission][4]
|
||||
|
||||
top 命令还为你提供有关使用了多少交换空间的实时更新。
|
||||
|
||||
### free
|
||||
|
||||
然而有时候,top 命令可能不会满足你的需求。你可能只需要查看系统的可用和已用内存。对此,Linux 还有 free 命令。free 命令显示:
|
||||
(to 校正者:以下这种可能翻译得不太准确,望纠正)
|
||||
* 可用和已使用的物理内存总量
|
||||
|
||||
* 系统中交换内存的总量
|
||||
|
||||
* 内核使用的缓冲区和缓存
|
||||
|
||||
在终端窗口中,输入 free 命令。它的输出不是实时的,相反,你将获得的是当前空闲和已用内存的即时快照(图 3)。
|
||||
|
||||
![free][9]
|
||||
|
||||
图 3 :free 命令的输出简单明了。
|
||||
|
||||
[Used with permission][4]
|
||||
|
||||
当然,你可以通过添加 -m 选项来让 free 显示得更友好一点,就像这样:free -m。这将显示内存的使用情况,以 MB 为单位(图 4)。
|
||||
|
||||
![free][11]
|
||||
|
||||
图 4:free 命令以一种更易于阅读的形式输出。
|
||||
|
||||
[Used with permission][4]
|
||||
|
||||
当然,如果你的系统是远程的,你将希望使用 -g 选项(以 GB 为单位),比如 free -g。
|
||||
|
||||
如果你需要内存总量,你可以添加 t 选项,比如:free -mt。这将简单地计算每列中的内存总量(图 5)。
|
||||
|
||||
![total][13]
|
||||
|
||||
图 5:为你提供空闲的内存列。
|
||||
|
||||
[Used with permission][4]
|
||||
|
||||
### vmstat
|
||||
|
||||
另一个非常方便的工具是 vmstat。这个特殊的命令是一个报告虚拟内存统计信息的小技巧。vmstat 命令将报告关于:
|
||||
|
||||
* 进程
|
||||
|
||||
* 内存
|
||||
|
||||
* 页
|
||||
|
||||
* 阻塞 IO
|
||||
|
||||
* traps
|
||||
|
||||
* 磁盘
|
||||
|
||||
* CPU
|
||||
|
||||
使用 vmstat 的最佳方法是使用 -s 选项,如 vmstat -s。这将在单列中报告统计信息(这比默认报告更容易阅读)。vmstat 命令将提供比你需要的更多的信息(图 6),但更多的总是更好的(在这种情况下)。
|
||||
|
||||
![vmstat][15]
|
||||
|
||||
图 6:使用 vmstat 命令来检查内存使用情况。
|
||||
|
||||
[Used with permission][4]
|
||||
|
||||
### dmidecode
|
||||
|
||||
如果你想找到关于已安装的系统 RAM 的详细信息,该怎么办?为此,你可以使用 dmidecode 命令。这个特殊的工具是 DMI 表解码器,它将系统的 DMI 表内容转储成人类可读的格式。如果你不清楚 DMI 表是什么,那么可以这样说,(to 校正者:这句话可以不加)它是可以用来描述系统的构成(以及系统的演变)。
|
||||
|
||||
要运行 dmidecode 命令,你需要 sudo 权限。因此输入命令 sudo dmidecode -t 17。该命令的输出(图 7)可能很长,因为它显示所有内存类型设备的信息。因此,如果你无法上下滚动,则可能需要将该命令的输出发送到一个文件中,比如:sudo dmidecode -t 17> dmi_infoI,或将其传递给 less 命令,如 sudo dmidecode | less。
|
||||
|
||||
![dmidecode][17]
|
||||
|
||||
图 7:dmidecode 命令的输出。
|
||||
|
||||
[Used with permission][4]
|
||||
|
||||
### /proc/meminfo
|
||||
|
||||
你可能会问自己:“这些命令从哪里获取这些信息?”在某些情况下,它们从 /proc/meminfo 文件中获取。你猜猜?你可以使用命令 less /proc/meminfo 直接读取该文件。通过使用 less 命令,你可以在长长的输出中向上和向下滚动,以准确找到你需要的内容(图 8)。
|
||||
|
||||
![/proc/meminfo][19]
|
||||
|
||||
图 8:less /proc/meminfo 命令的输出。
|
||||
|
||||
[Used with permission][4]
|
||||
|
||||
关于 /proc/meminfo 你应该知道:这不是一个真实的文件。相反 /pro/meminfo 是一个虚拟文件,包含有关系统的实时动态信息。特别是,你需要检查以下值:
|
||||
|
||||
* 全部内存
|
||||
|
||||
* 空闲内存
|
||||
|
||||
* 可用内存
|
||||
|
||||
* 缓冲区
|
||||
|
||||
* 文件缓存
|
||||
|
||||
* 交换缓存
|
||||
|
||||
* 全部交换区
|
||||
|
||||
* 空闲交换区
|
||||
|
||||
如果你想使用 /proc/meminfo,你可以用连词像 egrep 命令一样使用它:egrep --color'Mem | Cache | Swap'/proc/meminfo。这将生成一个易于阅读的列表,其中包含 Mem, Cache 和 Swap 等所有包含颜色的条目(图 9)。
|
||||
|
||||
![/proc/meminfo][21]
|
||||
|
||||
图 9:让 /proc/meminfo 更容易阅读。
|
||||
|
||||
[Used with permission][4]
|
||||
|
||||
### 继续学习
|
||||
|
||||
你要做的第一件事就是阅读每个命令的手册页(例如 man top, man free, man vmstat, man dmidecode)。从命令的手册页开始,对于如何在 Linux 上使用一个工具,它总是一个很好的学习方法。
|
||||
|
||||
通过 Linux 基金会和 edX 的免费 [“Linux 简介”][22]课程了解有关 Linux 的更多知识。
|
||||
|
||||
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.linux.com/learn/5-commands-checking-memory-usage-linux
|
||||
|
||||
作者:[Jack Wallen][a]
|
||||
选题:[lujun9972](https://github.com/lujun9972)
|
||||
译者:[MjSeven](https://github.com/MjSeven)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:https://www.linux.com/users/jlwallen
|
||||
[1]:https://www.ubuntu.com/download/server
|
||||
[2]:/files/images/memory1jpg
|
||||
[3]:https://www.linux.com/sites/lcom/files/styles/rendered_file/public/memory_1.jpg?itok=fhhhUL_l (top)
|
||||
[4]:/licenses/category/used-permission
|
||||
[5]:https://system76.com/desktops/leopard
|
||||
[6]:/files/images/memory2jpg
|
||||
[7]:https://www.linux.com/sites/lcom/files/styles/rendered_file/public/memory_2.jpg?itok=zuVkQfvv (top)
|
||||
[8]:/files/images/memory3jpg
|
||||
[9]:https://www.linux.com/sites/lcom/files/styles/rendered_file/public/memory_3.jpg?itok=rvuQp3t0 (free)
|
||||
[10]:/files/images/memory4jpg
|
||||
[11]:https://www.linux.com/sites/lcom/files/styles/rendered_file/public/memory_4.jpg?itok=K_luLLPt (free)
|
||||
[12]:/files/images/memory5jpg
|
||||
[13]:https://www.linux.com/sites/lcom/files/styles/rendered_file/public/memory_5.jpg?itok=q50atcsX (total)
|
||||
[14]:/files/images/memory6jpg
|
||||
[15]:https://www.linux.com/sites/lcom/files/styles/rendered_file/public/memory_6.jpg?itok=bwFnUVmy (vmstat)
|
||||
[16]:/files/images/memory7jpg
|
||||
[17]:https://www.linux.com/sites/lcom/files/styles/rendered_file/public/memory_7.jpg?itok=UNHIT_P6 (dmidecode)
|
||||
[18]:/files/images/memory8jpg
|
||||
[19]:https://www.linux.com/sites/lcom/files/styles/rendered_file/public/memory_8.jpg?itok=t87jvmJJ (/proc/meminfo)
|
||||
[20]:/files/images/memory9jpg
|
||||
[21]:https://www.linux.com/sites/lcom/files/styles/rendered_file/public/memory_9.jpg?itok=t-iSMEKq (/proc/meminfo)
|
||||
[22]:https://training.linuxfoundation.org/linux-courses/system-administration-training/introduction-to-linux
|
Loading…
Reference in New Issue
Block a user