如何使用 Linux 命令获得可用内存的百分比? [英] How to get the percentage of memory free with a Linux command?

查看:43
本文介绍了如何使用 Linux 命令获得可用内存的百分比?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Linux 命令行以百分比形式报告可用内存.

I would like to get the available memory reported as a percentage using a Linux command line.

我使用了 free 命令,但这只能给我数字,没有百分比选项.

I used the free command, but that is only giving me numbers, and there is no option for percentage.

推荐答案

使用 free 命令:

% free
             total       used       free     shared    buffers     cached
Mem:       2061712     490924    1570788          0      60984     220236
-/+ buffers/cache:     209704    1852008
Swap:       587768          0     587768

基于此输出,我们使用 Mem 抓取该行并使用 awk 选择特定字段进行计算.

Based on this output we grab the line with Mem and using awk pick specific fields for our computations.

这将报告正在使用的内存百分比

This will report the percentage of memory in use

% free | grep Mem | awk '{print $3/$2 * 100.0}'
23.8171

这将报告空闲内存的百分比

This will report the percentage of memory that's free

% free | grep Mem | awk '{print $4/$2 * 100.0}'
76.5013

您可以为此命令创建别名或将其放入一个很小的 ​​shell 脚本中.可以使用打印语句的格式化命令来根据您的需要定制特定的输出:

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:

free | grep Mem | awk '{ printf("free: %.4f %
", $4/$2 * 100.0) }'

这篇关于如何使用 Linux 命令获得可用内存的百分比?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆