如何找出适合由 shell 脚本解析的 linux 机器的总物理内存 (RAM)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20348007/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:31:58  来源:igfitidea点击:

How can I find out the total physical memory (RAM) of my linux box suitable to be parsed by a shell script?

linuxrammemory-size

提问by Jdamian

I'm typing a shell script to find out the total physical memory in some RHEL linux boxes.

我正在键入一个 shell 脚本来找出某些 RHEL linux 机器中的总物理内存。

First of all I want to stress that I'm interested in the total physical memoryrecognized by kernel, not just the available memory. Therefore, please, avoid answers suggesting to read /proc/meminfoor to use the free, topor sarcommands -- In all these cases, their "total memory" values mean "available memory" ones.

首先我想强调的是,我对内核识别的总物理内存感兴趣,而不仅仅是可用内存。因此,请避免建议阅读/proc/meminfo或使用freetopsar命令的答案——在所有这些情况下,它们的“总内存”值意味着“可用内存”值。

The first thought was to read the boot kernel messages:

第一个想法是阅读引导内核消息:

Memory: 61861540k/63438844k available (2577k kernel code, 1042516k reserved, 1305k data, 212k init)

But in some linux boxes, due to the use of EMC2's PowerPath software and its floodingboot messages in the kernel startup, that useful boot kernel message is not available, not even in the /var/log/dmesgfile.

但是在某些 linux 系统中,由于使用了 EMC2 的 PowerPath 软件及其在内核启动时泛滥的引导消息,有用的引导内核消息不可用,甚至在/var/log/dmesg文件中也不可用。

The second option was the dmidecodecommand (I'm warned against the possible mismatch of kernel recognized RAM and real RAM due to the limitations of some older kernels and architectures). The option --memorysimplifies the script but I realized that older releases of that command has no --memoryoption.

第二个选项是dmidecode命令(由于某些旧内核和架构的限制,我警告说内核识别的 RAM 和实际 RAM 可能不匹配)。选项--memory简化了脚本,但我意识到该命令的旧版本没有--memory选项。

My last chance was the getconfcommand. It reports the memory page size, but not the total number of physical pages -- the _PHYS_PAGESsystem variable seems to be the available physical pages, not the total physical pages.

我最后的机会是getconf命令。它报告内存页面大小,但不报告物理页面的总数——_PHYS_PAGES系统变量似乎是可用的物理页面,而不是总物理页面。

# getconf -a | grep PAGES
PAGESIZE                           4096
_AVPHYS_PAGES                      1049978
_PHYS_PAGES                        15466409

My question: Is there another way to get the total amount of physical memory, suitable to be parsed by a shell script?

我的问题:有没有另一种方法可以获得物理内存总量,适合由shell脚本解析?

采纳答案by Alex

If you're interested in the physical RAM, use the command dmidecode. It gives you a lotmore information than just that, but depending on your use case, you might also want to know if the 8G in the system come from 2x4GB sticks or 4x2GB sticks.

如果您对物理 RAM 感兴趣,请使用命令dmidecode。它给你一个很大的不仅仅是更多的信息,但根据您的使用情况下,你可能也想知道,如果8G系统来自2x4GB棒或4x2GB棒。

回答by Alex

Have you tried cat /proc/meminfo? You can then awkor grepout what you want, MemTotale.g.

你试过cat /proc/meminfo吗?然后,您可以使用awkgrep找出您想要的内容,例如MemTotal

awk '/MemTotal/ {print }' /proc/meminfo

or

或者

cat /proc/meminfo | grep MemTotal

回答by Sathyam

I find htopa useful tool.

我找到htop了一个有用的工具。

sudo apt-get install htop

sudo apt-get install htop

and then

进而

free -m

自由 -m

will give the information you need.

将提供您需要的信息。

回答by Sandip

Add the last 2 entries of /proc/meminfo, they give you the exact memory present on the host.

添加 的最后 2 个条目/proc/meminfo,它们为您提供主机上存在的确切内存。

Example:

例子:

DirectMap4k:       10240 kB
DirectMap2M:     4184064 kB

10240 + 4184064 = 4194304 kB = 4096 MB.

10240 + 4184064 = 4194304 kB = 4096 MB。

回答by Cyril

cat /proc/meminfo | grep MemTotalor free gives you the exact amount of RAM your server has. This is not "available memory".

cat /proc/meminfo | grep MemTotal或 free 为您提供服务器拥有的确切 RAM 量。这不是“可用内存”。

I guess your issue comes up when you have a VM and you would like to calculate the full amount of memory hosted by the hypervisor but you will have to log into the hypervisor in that case.

我想当您拥有 VM 并且您想计算虚拟机管理程序托管的全部内存量时,您的问题就会出现,但在这种情况下您必须登录虚拟机管理程序。

cat /proc/meminfo | grep MemTotal

is equivalent to

相当于

 getconf -a | grep PAGES | awk 'BEGIN {total = 1} {if (NR == 1 || NR == 3) total *=$NF} END {print total / 1024" kB"}'

回答by Yogesh Jilhawar

One more useful command:
vmstat -s | grep memory
sample output on my machine is:

一个更有用的命令:
vmstat -s | grep memory
我机器上的示例输出是:

  2050060 K total memory
  1092992 K used memory
   743072 K active memory
   177084 K inactive memory
   957068 K free memory
   385388 K buffer memory

another useful command to get memory information is:
free
sample output is:

另一个获取内存信息的有用命令是:
free
示例输出是:

             total       used       free     shared    buffers     cached
Mem:       2050060    1093324     956736        108     385392     386812
-/+ buffers/cache:     321120    1728940
Swap:      2095100       2732    2092368

One observation here is that, the command freegives information about swap space also.
The following link may be useful for you:
http://www.linuxnix.com/find-ram-details-in-linuxunix/

这里的一个观察结果是,该命令还free提供了有关交换空间的信息。
以下链接可能对您有用:http:
//www.linuxnix.com/find-ram-details-in-linuxunix/

回答by JayEek

free -h | awk '/Mem\:/ { print  }' 

This will provide you with the total memory in your system in human readable format and automatically scale to the appropriate unit ( e.g. bytes, KB, MB, or GB).

这将以人类可读的格式为您提供系统中的总内存,并自动缩放到适当的单位(例如字节、KB、MB 或 GB)。

回答by Raghu Ni

dmidecode -t 17 | grep  Size:

Adding all above values displayed after "Size: " will give exact total physical size of all RAM sticks in server.

添加“大小:”之后显示的所有上述值将给出服务器中所有 RAM 棒的确切总物理大小。

回答by Sandeep_black

These are the ways :

这些是方法:

1. /proc/meminfo

1. /proc/meminfo

MemTotal: 8152200 kB

MemFree: 760808 kB

You can write a code or script to parse it.

您可以编写代码或脚本来解析它。

2. Use sysconf by using below macros

2. 使用以下宏来使用 sysconf

sysconf (_SC_PHYS_PAGES) * sysconf (_SC_PAGESIZE);

系统配置(_SC_PHYS_PAGES)* 系统配置(_SC_PAGESIZE);

3. By using sysinfo system call

3. 通过使用 sysinfo 系统调用

int sysinfo(struct sysinfo *info);

int sysinfo(struct sysinfo *info);

struct sysinfo { .

结构系统信息{。

   .

   unsigned long totalram;  /*Total memory size to use */

   unsigned long freeram;   /* Available memory size*/

   .

   . 

  }; 

回答by Eduardo Cuomo

Total memory in Mb:

总内存Mb

x=$(awk '/MemTotal/ {print }' /proc/meminfo)
echo $((x/1024))

or:

或者:

x=$(awk '/MemTotal/ {print }' /proc/meminfo) ; echo $((x/1024))