使用单个 bash shell 命令获取 gb 中的可用内存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34937580/
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
get available memory in gb using single bash shell command
提问by Haris Farooqui
following command returns available memory in kilobytes
以下命令以千字节为单位返回可用内存
cat /proc/meminfo | grep MemFree | awk '{ print $2 }'
cat /proc/meminfo | grep MemFree | awk '{ print $2 }'
can some one suggest single command to get the available memory in gb?
有人可以建议使用单个命令来获取 gb 中的可用内存吗?
回答by tink
Just a slight modification to your own magical incantation:
只需对您自己的魔法咒语稍作修改:
awk '/MemFree/ { printf "%.3f \n", /1024/1024 }' /proc/meminfo
P.S.: Dear OP, if you find yourself invoking grep & awk in one line you're most likely doing it wrong ;} ... Same with invoking cat on a single file; that's hardly ever warranted.
PS:亲爱的 OP,如果您发现自己在一行中调用了 grep 和 awk,那么您很可能做错了;} ... 与在单个文件上调用 cat 相同;这几乎没有必要。
回答by Rany Albeg Wein
freemem_in_gb () {
read -r _ freemem _ <<< "$(grep --fixed-strings 'MemFree' /proc/meminfo)"
bc <<< "scale=3;${freemem}/1024/1024"
}
Please notice that scale=3
can be changed to some other value, for a better precision.
So, for example one could write a function that will take a precision argument, like so:
请注意,scale=3
可以更改为其他值,以获得更好的精度。因此,例如,可以编写一个接受精度参数的函数,如下所示:
freemem_in_gb () {
prec=;
read -r _ freemem _ <<< "$(grep --fixed-strings 'MemFree' /proc/meminfo)"
bc <<< "scale=${prec:-3};${freemem}/1024/1024"
}
Which will take (or use 3 as a default value) and pass a precision argument to bc
's scale
option
这将采用(或使用 3 作为默认值)并将精度参数传递给bc
'sscale
选项
Usage example:
用法示例:
$ freemem_in_gb
5.524
$ freemem_in_gb 7
5.5115814
EDITThanks for @Stephen P and @Etan Reisner for leaving a comment and improving this answer. Code edited accordingly.
编辑感谢@Stephen P 和@Etan Reisner 发表评论并改进此答案。相应地编辑了代码。
grep
's long option --fixed-strings
is used purposely instead of -F
or fgrep
for explanatory reasons.
grep
的长选项--fixed-strings
使用的故意代替-F
或fgrep
出于解释的原因。
回答by Amitesh
Most simple is the following :
最简单的是以下内容:
free -h
Following is the output Screenshot :
以下是输出截图:
More details :
更多细节 :
DESCRIPTION
描述
free - displays the total amount of free and used physical and swap mem‐
ory in the system, as well as the buffers and caches used by the ker‐
nel. The information is gathered by parsing /proc/meminfo. The dis‐
played columns are:
total Total installed memory (MemTotal and SwapTotal in /proc/meminfo)
used Used memory (calculated as total - free - buffers - cache)
free Unused memory (MemFree and SwapFree in /proc/meminfo)
shared Memory used (mostly) by tmpfs (Shmem in /proc/meminfo, available
on kernels 2.6.32, displayed as zero if not available)
buffers
Memory used by kernel buffers (Buffers in /proc/meminfo)
cache Memory used by the page cache and slabs (Cached and Slab in
/proc/meminfo)
buff/cache
Sum of buffers and cache
available
Estimation of how much memory is available for starting new
applications, without swapping. Unlike the data provided by the
cache or free fields, this field takes into account page cache
and also that not all reclaimable memory slabs will be reclaimed
due to items being in use (MemAvailable in /proc/meminfo, avail‐
able on kernels 3.14, emulated on kernels 2.6.27+, otherwise the
same as free)
回答by radtek
If you have python, you can do it this way:
如果你有 python,你可以这样做:
python -c "import os;print int(round(os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES') / 1024.0**3))"
In this example, I used round
to round to the nearest GB. You can make it into a shell function like so:
在这个例子中,我曾经round
四舍五入到最近的 GB。你可以把它变成一个 shell 函数,如下所示:
get_mem(){
MEM=$(python -c "import os;print int(round(os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES') / 1024.0**3))")
echo $MEM
}