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

查看:898
本文介绍了如何使用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脚本中.可以使用以下语句针对print语句的格式化命令来根据您的需求量身定制特定的输出:

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 %\n", $4/$2 * 100.0) }'

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

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