mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-01 21:50:13 +08:00
Merge pull request #7148 from jessie-pang/master
Linux Free Command Explained for Beginners (6 Examples).md
This commit is contained in:
commit
53e96eccec
@ -1,122 +0,0 @@
|
||||
Translating by jessie-pang
|
||||
|
||||
Linux Free Command Explained for Beginners (6 Examples)
|
||||
======
|
||||
|
||||
Sometimes, while working on the command line in Linux, you might want to quickly take a look at the total available as well as used memory in the system. If you're a Linux newbie, you'll be glad to know there exists a built-in command - dubbed **free** \- that displays this kind of information.
|
||||
|
||||
In this tutorial, we will discuss the basics of the free command as well as some of the important features it provides. But before we do that, it's worth sharing that all commands/instructions mentioned here have been tested on Ubuntu 16.04LTS.
|
||||
|
||||
### Linux free command
|
||||
|
||||
Here's the syntax of the free command:
|
||||
|
||||
free [options]
|
||||
|
||||
And following is how the tool's man page describes it:
|
||||
```
|
||||
free displays the total amount of free and used physical and swap memory in the system, as well as
|
||||
the buffers and caches used by the kernel. The information is gathered by parsing
|
||||
/proc/meminfo.
|
||||
```
|
||||
|
||||
Following are some Q&A-styled examples that should give you a good idea about how the free command works.
|
||||
|
||||
### Q1. How to view used and available memory using free command?
|
||||
|
||||
This is very easy. All you have to do is to run the free command without any options.
|
||||
|
||||
free
|
||||
|
||||
Here's the output the free command produced on my system:
|
||||
|
||||
[![view used and available memory using free command][1]][2]
|
||||
|
||||
And here's what these columns mean:
|
||||
|
||||
[![Free command columns][3]][4]
|
||||
|
||||
### Q2. How to change the display metric?
|
||||
|
||||
If you want, you can change the display metric of memory figures that the free command produces in output. For example, if you want to display memory in megabytes, you can use the **-m** command line option.
|
||||
|
||||
free -m
|
||||
|
||||
[![free command display metrics change][5]][6]
|
||||
|
||||
Similarly, you can use **-b** for bytes, **-k** for kilobytes, **-m** for megabytes, **-g** for gigabytes, **\--tera** for terabytes.
|
||||
|
||||
### Q3. How to display memory figures in human readable form?
|
||||
|
||||
The free command also offers an option **-h** through which you can ask the tool to display memory figures in human-readable form.
|
||||
|
||||
free -h
|
||||
|
||||
With this option turned on, the command decides for itself which display metric to use for individual memory figures. For example, here's how the -h option worked in our case:
|
||||
|
||||
[![diplsy data fromm free command in human readable form][7]][8]
|
||||
|
||||
### Q4. How to make free display results continuously with time gap?
|
||||
|
||||
If you want, you can also have the free command executed in a way that it continuously displays output after a set time gap. For this, use the **-s** command line option. This option requires user to pass a numeric value that will be treated as the number of seconds after which output will be displayed.
|
||||
|
||||
For example, to keep a gap of 3 seconds, run the command in the following way:
|
||||
|
||||
free -s 3
|
||||
|
||||
In this setup, if you want free to run only a set number of times, you can use the **-c** command option, which requires a count value to be passed to it. For example:
|
||||
|
||||
free -s 3 -c 5
|
||||
|
||||
The aforementioned command will make sure the tool runs 5 times, with a 3 second time gap between each of the tries.
|
||||
|
||||
**Note** : This functionality is currently [buggy][9], so we couldn't test it at our end.
|
||||
|
||||
### Q5. How to make free use power of 1000 (not 1024) while displaying memory figures?
|
||||
|
||||
If you change the display metric to say megabytes (using -m option), but want the figures to be calculated based on power of 1,000 (not 1024), then this can be done using the **\--si** option. For example, the following screenshot shows the difference in output with and without this option:
|
||||
|
||||
[![How to make free use power of 1000 \(not 1024\) while displaying memory figures][10]][11]
|
||||
|
||||
### Q6. How to make free display total of columns?
|
||||
|
||||
If you want free to display a total of all memory figures in each column, then you can use the **-t** command line option.
|
||||
|
||||
free -t
|
||||
|
||||
Following screenshot shows this command line option in action:
|
||||
|
||||
[![How to make free display total of columns][12]][13]
|
||||
|
||||
Note the new 'Total' row that's displayed in this case.
|
||||
|
||||
### Conclusion
|
||||
|
||||
The free command can prove to be an extremely useful tool if you're into system administration. It's easy to understand and use, with many options to customize output. We've covered many useful options in this tutorial. After you're done practicing these, head to the command's [man page][14] for more.
|
||||
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.howtoforge.com/linux-free-command/
|
||||
|
||||
作者:[Himanshu Arora][a]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:https://www.howtoforge.com
|
||||
[1]:https://www.howtoforge.com/images/linux_free_command/free-command-output.png
|
||||
[2]:https://www.howtoforge.com/images/linux_free_command/big/free-command-output.png
|
||||
[3]:https://www.howtoforge.com/images/linux_free_command/free-output-columns.png
|
||||
[4]:https://www.howtoforge.com/images/linux_free_command/big/free-output-columns.png
|
||||
[5]:https://www.howtoforge.com/images/linux_free_command/free-m-option.png
|
||||
[6]:https://www.howtoforge.com/images/linux_free_command/big/free-m-option.png
|
||||
[7]:https://www.howtoforge.com/images/linux_free_command/free-h.png
|
||||
[8]:https://www.howtoforge.com/images/linux_free_command/big/free-h.png
|
||||
[9]:https://bugs.launchpad.net/ubuntu/+source/procps/+bug/1551731
|
||||
[10]:https://www.howtoforge.com/images/linux_free_command/free-si-option.png
|
||||
[11]:https://www.howtoforge.com/images/linux_free_command/big/free-si-option.png
|
||||
[12]:https://www.howtoforge.com/images/linux_free_command/free-t-option.png
|
||||
[13]:https://www.howtoforge.com/images/linux_free_command/big/free-t-option.png
|
||||
[14]:https://linux.die.net/man/1/free
|
@ -0,0 +1,135 @@
|
||||
6 个例子让初学者掌握 free 命令
|
||||
======
|
||||
|
||||
在 Linux 系统上,有时你可能想从命令行快速地了解系统的已使用和未使用的内存空间。如果你是一个 Linux 新手,有个好消息:有一条系统内置的命令可以显示这些信息:**free**。
|
||||
|
||||
在本文中,我们会讲到 free 命令的基本用法以及它所提供的一些重要的功能。文中提到的所有命令和用法都是在 Ubuntu 16.04LTS 上测试过的。
|
||||
|
||||
### Linux free 命令
|
||||
|
||||
让我们看一下 free 命令的语法:
|
||||
|
||||
free [options]
|
||||
|
||||
free 命令的 man 手册如是说:
|
||||
|
||||
```
|
||||
free 命令显示了系统的可用和已用的物理内存及交换内存的总量,以及内核用到的缓存空间。这些信息是从 /proc/meminfo 中得到的。
|
||||
```
|
||||
|
||||
接下来我们用问答的方式了解一下 free 命令是怎么工作的。
|
||||
|
||||
### Q1. 怎么用 free 命令查看已使用和未使用的内存?
|
||||
|
||||
这很容易,您只需不加任何参数地运行 free 这条命令就可以了:
|
||||
|
||||
free
|
||||
|
||||
这是 free 命令在我的系统上的输出:
|
||||
|
||||
[![view used and available memory using free command][1]][2]
|
||||
|
||||
这些列是什么意思呢?
|
||||
|
||||
[![Free command columns][3]][4]
|
||||
|
||||
total - 安装的内存的总量(等同于 /proc/meminfo 中的 MemTotal 和 SwapTotal)
|
||||
|
||||
used - 已使用的内存(计算公式为:total - free - buffers - cache)
|
||||
|
||||
free - 未被使用的内存(等同于 /proc/meminfo 中的 MemFree 和 SwapFree)
|
||||
|
||||
shared - 通常是临时文件系统使用的内存(等同于 /proc/meminfo 中的 Shmem;在内核 2.6.32 版本上生效,参数无效则显示为 0)
|
||||
|
||||
buffers - 内核缓冲区使用的内存(等同于 /proc/meminfo 中的 Buffers)
|
||||
|
||||
cache - 页面缓存和 Slab 分配机制使用的内存(等同于 /proc/meminfo 中的 Cached 和 Slab)
|
||||
|
||||
buff/cache - buffers 与 cache 之和
|
||||
|
||||
available - 在不计算交换空间的情况下,预计可以被新启动的应用程序所使用的内存空间。与 cache 或者 free 部分不同,这一列把页面缓存计算在内,并且不是所有的可回收 slab 内存都可以真正被回收,因为可能有被占用的部分。(等同于 /proc/meminfo 中的 MemAvailable;在内核 3.14 版本上生效,从内核 2.6.27 版本开始仿真;在其他版本上这个值与 free 这一列相同)
|
||||
|
||||
### Q2. 如何更改显示的单位呢?
|
||||
|
||||
如果需要的话,你可以更改内存的显示单位。比如说,想要内存以兆为单位显示,你可以用 **-m** 这个参数:
|
||||
|
||||
free -m
|
||||
|
||||
[![free command display metrics change][5]][6]
|
||||
|
||||
同样地,你可以用 **-b** 以字节显示、**-k** 以 KB 显示、**-m** 以 MB 显示、**-g** 以 GB 显示、**\--tera** 以 TB 显示。
|
||||
|
||||
### Q3. 怎么显示可读的结果呢?
|
||||
|
||||
free 命令提供了 **-h** 这个参数使输出转化为可读的格式。
|
||||
|
||||
free -h
|
||||
|
||||
用这个参数,free 命令会自己决定用什么单位显示内存的每个数值。例如:
|
||||
|
||||
[![diplsy data fromm free command in human readable form][7]][8]
|
||||
|
||||
### Q4. 怎么让 free 命令以一定的时间间隔持续运行?
|
||||
|
||||
您可以用 **-s** 这个参数让 free 命令以一定的时间间隔持续地执行。您需要传递给命令行一个数字参数,做为这个时间间隔的秒数。
|
||||
|
||||
例如,使 free 命令每隔 3 秒执行一次:
|
||||
|
||||
free -s 3
|
||||
|
||||
如果您需要 free 命令只执行几次,您可以用 **-c** 这个参数指定执行的次数:
|
||||
|
||||
free -s 3 -c 5
|
||||
|
||||
上面这条命令可以确保 free 命令每隔 3 秒执行一次,总共执行 5 次。
|
||||
|
||||
**注**:这个功能目前在Ubuntu系统上还存在 [问题][9],所以并未测试。
|
||||
|
||||
### Q5. 怎么使 free 基于 1000 计算内存,而不是 1024?
|
||||
|
||||
如果您指定 free 用 MB 来显示内存(用 -m 参数),但又想基于 1000 来计算结果,可以用 **\--sj** 这个参数来实现。下图展示了用与不用这个参数的结果:
|
||||
|
||||
[![How to make free use power of 1000 \(not 1024\) while displaying memory figures][10]][11]
|
||||
|
||||
### Q6. 如何使 free 命令显示每一列的总和?
|
||||
|
||||
如果您想要 free 命令显示每一列的总和,你可以用 **-t** 这个参数。
|
||||
|
||||
free -t
|
||||
|
||||
如下图所示:
|
||||
|
||||
[![How to make free display total of columns][12]][13]
|
||||
|
||||
请注意 “Total” 这一行出现了。
|
||||
|
||||
### 总结
|
||||
|
||||
free 命令对于系统管理来讲是个极其有用的工具。它有很多参数可以定制化您的输出,易懂易用。我们在本文中也提到了很多有用的参数。练习完之后,请您移步至 [man 手册][14]了解更多内容。
|
||||
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.howtoforge.com/linux-free-command/
|
||||
|
||||
作者:[Himanshu Arora][a]
|
||||
译者:[jessie-pang](https://github.com/jessie-pang)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:https://www.howtoforge.com
|
||||
[1]:https://www.howtoforge.com/images/linux_free_command/free-command-output.png
|
||||
[2]:https://www.howtoforge.com/images/linux_free_command/big/free-command-output.png
|
||||
[3]:https://www.howtoforge.com/images/linux_free_command/free-output-columns.png
|
||||
[4]:https://www.howtoforge.com/images/linux_free_command/big/free-output-columns.png
|
||||
[5]:https://www.howtoforge.com/images/linux_free_command/free-m-option.png
|
||||
[6]:https://www.howtoforge.com/images/linux_free_command/big/free-m-option.png
|
||||
[7]:https://www.howtoforge.com/images/linux_free_command/free-h.png
|
||||
[8]:https://www.howtoforge.com/images/linux_free_command/big/free-h.png
|
||||
[9]:https://bugs.launchpad.net/ubuntu/+source/procps/+bug/1551731
|
||||
[10]:https://www.howtoforge.com/images/linux_free_command/free-si-option.png
|
||||
[11]:https://www.howtoforge.com/images/linux_free_command/big/free-si-option.png
|
||||
[12]:https://www.howtoforge.com/images/linux_free_command/free-t-option.png
|
||||
[13]:https://www.howtoforge.com/images/linux_free_command/big/free-t-option.png
|
||||
[14]:https://linux.die.net/man/1/free
|
Loading…
Reference in New Issue
Block a user