Skip to main content

6 个例子让初学者掌握 free 命令

编译自 | https://www.howtoforge.com/linux-free-command/ 
 作者 | Himanshu Arora
 译者 | jessie-pang
在 Linux 系统上,有时你可能想从命令行快速地了解系统的已使用和未使用的内存空间。如果你是一个 Linux 新手,有个好消息:有一条系统内置的命令可以显示这些信息:free
在本文中,我们会讲到 free 命令的基本用法以及它所提供的一些重要的功能。文中提到的所有命令和用法都是在 Ubuntu 16.04LTS 上测试过的。
Linux free 命令
让我们看一下 free 命令的语法:
  1. free [options]
free 命令的 man 手册如是说:
free 命令显示了系统的可用和已用的物理内存及交换内存的总量,以及内核用到的缓存空间。这些信息是从 /proc/meminfo 中得到的。
接下来我们用问答的方式了解一下 free 命令是怎么工作的。
Q1. 怎么用 free 命令查看已使用和未使用的内存?
这很容易,您只需不加任何参数地运行 free 这条命令就可以了:
  1. free
这是 free 命令在我的系统上的输出:
view used and available memory using free command
这些列是什么意思呢?
◈ total - 安装的内存的总量(等同于 /proc/meminfo 中的 MemTotal 和 SwapTotal
◈ used - 已使用的内存(计算公式为: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这个参数:
  1. free -m
free command display metrics change
同样地,你可以用 -b 以字节显示、-k 以 KB 显示、-m 以 MB 显示、-g 以 GB 显示、--tera 以 TB 显示。
Q3. 怎么显示可读的结果呢?
free 命令提供了 -h 这个参数使输出转化为可读的格式。
  1. free -h
用这个参数,free 命令会自己决定用什么单位显示内存的每个数值。例如:
diplsy data fromm free command in human readable form
Q4. 怎么让 free 命令以一定的时间间隔持续运行?
您可以用 -s 这个参数让 free 命令以一定的时间间隔持续地执行。您需要传递给命令行一个数字参数,做为这个时间间隔的秒数。
例如,使 free 命令每隔 3 秒执行一次:
  1. free -s 3
如果您需要 free 命令只执行几次,您可以用 -c 这个参数指定执行的次数:
  1. free -s 3 -c 5
上面这条命令可以确保 free 命令每隔 3 秒执行一次,总共执行 5 次。
注:这个功能目前在 Ubuntu 系统上还存在 问题[1],所以并未测试。
Q5. 怎么使 free 基于 1000 计算内存,而不是 1024?
如果您指定 free 用 MB 来显示内存(用 -m 参数),但又想基于 1000 来计算结果,可以用 --sj 这个参数来实现。下图展示了用与不用这个参数的结果:
How to make free use power of 1000 \(not 1024\) while displaying memory figures
Q6. 如何使 free 命令显示每一列的总和?
如果您想要 free 命令显示每一列的总和,你可以用 -t 这个参数。
  1. free -t
如下图所示:
How to make free display total of columns
请注意 Total 这一行出现了。
总结
free 命令对于系统管理来讲是个极其有用的工具。它有很多参数可以定制化您的输出,易懂易用。我们在本文中也提到了很多有用的参数。练习完之后,请您移步至 man 手册[2]了解更多内容。

Comments

Popular posts from this blog

OWASP Top 10 Threats and Mitigations Exam - Single Select

Last updated 4 Aug 11 Course Title: OWASP Top 10 Threats and Mitigation Exam Questions - Single Select 1) Which of the following consequences is most likely to occur due to an injection attack? Spoofing Cross-site request forgery Denial of service   Correct Insecure direct object references 2) Your application is created using a language that does not support a clear distinction between code and data. Which vulnerability is most likely to occur in your application? Injection   Correct Insecure direct object references Failure to restrict URL access Insufficient transport layer protection 3) Which of the following scenarios is most likely to cause an injection attack? Unvalidated input is embedded in an instruction stream.   Correct Unvalidated input can be distinguished from valid instructions. A Web application does not validate a client’s access to a resource. A Web action performs an operation on behalf of the user without checking a shared sec

CKA Simulator Kubernetes 1.22

  https://killer.sh Pre Setup Once you've gained access to your terminal it might be wise to spend ~1 minute to setup your environment. You could set these: alias k = kubectl                         # will already be pre-configured export do = "--dry-run=client -o yaml"     # k get pod x $do export now = "--force --grace-period 0"   # k delete pod x $now Vim To make vim use 2 spaces for a tab edit ~/.vimrc to contain: set tabstop=2 set expandtab set shiftwidth=2 More setup suggestions are in the tips section .     Question 1 | Contexts Task weight: 1%   You have access to multiple clusters from your main terminal through kubectl contexts. Write all those context names into /opt/course/1/contexts . Next write a command to display the current context into /opt/course/1/context_default_kubectl.sh , the command should use kubectl . Finally write a second command doing the same thing into /opt/course/1/context_default_no_kubectl.sh , but without the use of k

标 题: 关于Daniel Guo 律师

发信人: q123452017 (水天一色), 信区: I140 标  题: 关于Daniel Guo 律师 关键字: Daniel Guo 发信站: BBS 未名空间站 (Thu Apr 26 02:11:35 2018, 美东) 这些是lz根据亲身经历在 Immigration版上发的帖以及一些关于Daniel Guo 律师的回 帖,希望大家不要被一些马甲帖广告帖所骗,慎重考虑选择律师。 WG 和Guo两家律师对比 1. fully refund的合约上的区别 wegreened家是case不过只要第二次没有file就可以fully refund。郭家是要两次case 没过才给refund,而且只要第二次pl draft好律师就可以不退任何律师费。 2. 回信速度 wegreened家一般24小时内回信。郭律师是在可以快速回复的时候才回复很快,对于需 要时间回复或者是不愿意给出确切答复的时候就回复的比较慢。 比如:lz问过郭律师他们律所在nsc区域最近eb1a的通过率,大家也知道nsc现在杀手如 云,但是郭律师过了两天只回复说让秘书update最近的case然后去网页上查,但是上面 并没有写明tsc还是nsc。 lz还问过郭律师关于准备ps (他要求的文件)的一些问题,模版上有的东西不是很清 楚,但是他一般就是把模版上的东西再copy一遍发过来。 3. 材料区别 (推荐信) 因为我只收到郭律师写的推荐信,所以可以比下两家推荐信 wegreened家推荐信写的比较长,而且每封推荐信会用不同的语气和风格,会包含lz写 的research summary里面的某个方面 郭家四封推荐信都是一个格式,一种语气,连地址,信的称呼都是一样的,怎么看四封 推荐信都是同一个人写出来的。套路基本都是第一段目的,第二段介绍推荐人,第三段 某篇或几篇文章的abstract,最后结论 4. 前期材料准备 wegreened家要按照他们的模版准备一个十几页的research summary。 郭律师在签约之前说的是只需要准备五页左右的summary,但是在lz签完约收到推荐信 ,郭律师又发来一个很长的ps要lz自己填,而且和pl的格式基本差不多。 总结下来,申请自己上心最重要。但是如果选律师,lz更倾向于wegreened,