How can I find out the total physical memory (RAM)

2019-01-29 21:44发布

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

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

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 flooding boot messages in the kernel startup, that useful boot kernel message is not available, not even in the /var/log/dmesg file.

The second option was the dmidecode command (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 --memory simplifies the script but I realized that older releases of that command has no --memory option.

My last chance was the getconf command. It reports the memory page size, but not the total number of physical pages -- the _PHYS_PAGES system variable seems to be the available physical pages, not the total physical 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?

11条回答
淡お忘
2楼-- · 2019-01-29 21:52
dmidecode -t 17 | grep  Size:

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

查看更多
We Are One
3楼-- · 2019-01-29 21:53

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

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.

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"}'
查看更多
Fickle 薄情
4楼-- · 2019-01-29 21:53

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

Example:

DirectMap4k:       10240 kB
DirectMap2M:     4184064 kB

10240 + 4184064 = 4194304 kB = 4096 MB.

查看更多
混吃等死
5楼-- · 2019-01-29 21:55

I find htop a useful tool.

sudo apt-get install htop

and then

free -m

will give the information you need.

查看更多
一夜七次
6楼-- · 2019-01-29 21:59

Have you tried cat /proc/meminfo? You can then grep out what you want, MemTotal e.g.

Updated Example (btw. thanks, Masta):

awk '/MemTotal/ {print $2}' /proc/meminfo
查看更多
你好瞎i
7楼-- · 2019-01-29 22:03

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

  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:

             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 free gives information about swap space also.
The following link may be useful for you:
http://www.linuxnix.com/find-ram-details-in-linuxunix/

查看更多
登录 后发表回答