如何使用 Linux 命令获得可用内存的百分比?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10585978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the percentage of memory free with a Linux command?
提问by Timothy Clemans
I would like to get the available memory reported as a percentage using a Linux command line.
我想使用 Linux 命令行以百分比形式报告可用内存。
I used the free
command, but that is only giving me numbers, and there is no option for percentage.
我使用了free
命令,但那只给了我数字,没有百分比选项。
采纳答案by Levon
Using the the free
command:
使用free
命令:
% free
total used free shared buffers cached
Mem: 2061712 490924 1570788 0 60984 220236
-/+ buffers/cache: 209704 1852008
Swap: 587768 0 587768
Based on this output we grab the line with Mem
and using awk pick specific fields for our computations.
基于此输出,我们Mem
使用 awk 选择特定字段进行计算。
This will report the percentage of memory in use
这将报告正在使用的内存百分比
% free | grep Mem | awk '{print / * 100.0}'
23.8171
This will report the percentage of memory that's free
这将报告可用内存的百分比
% free | grep Mem | awk '{print / * 100.0}'
76.5013
You could create an alias for this command or put this into a tiny shell script. The specific output could be tailored to your needs using formatting commands for the print statement along these lines:
您可以为此命令创建别名或将其放入一个很小的 shell 脚本中。可以使用打印语句的格式化命令按照以下几行来定制特定的输出:
free | grep Mem | awk '{ printf("free: %.4f %\n", / * 100.0) }'