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

前端 未结 13 1793
梦如初夏
梦如初夏 2020-12-07 09:52

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 ph

相关标签:
13条回答
  • Have you tried cat /proc/meminfo? You can then awk or grep out what you want, MemTotal e.g.

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

    or

    cat /proc/meminfo | grep MemTotal
    
    0 讨论(0)
  • 2020-12-07 10:10

    I know this question was asked a long time ago, but I wanted to provide another way to do this that I found useful for an issue I just worked on:

    lshw -c memory
    

    lshw

    lshw is a small tool to extract detailed information on the hardware configuration of the machine. It can report exact memory configuration, firmware version, mainboard configuration, CPU version and speed, cache configuration, bus speed, etc. on DMI-capable x86 or IA-64 systems and on some PowerPC machines (PowerMac G4 is known to work).

    0 讨论(0)
  • 2020-12-07 10:15

    Total online memory

    Calculate the total online memory using the sys-fs.

    totalmem=0;
    for mem in /sys/devices/system/memory/memory*; do
      [[ "$(cat ${mem}/online)" == "1" ]] \
        && totalmem=$((totalmem+$((0x$(cat /sys/devices/system/memory/block_size_bytes)))));
    done
    
    #one-line code
    totalmem=0; for mem in /sys/devices/system/memory/memory*; do [[ "$(cat ${mem}/online)" == "1" ]] && totalmem=$((totalmem+$((0x$(cat /sys/devices/system/memory/block_size_bytes))))); done
    
    echo ${totalmem} bytes
    echo $((totalmem/1024**3)) GB
    

    Example output for 4 GB system:

    4294967296 bytes
    4 GB
    

    Explanation

    /sys/devices/system/memory/block_size_bytes

    Number of bytes in a memory block (hex value). Using 0x in front of the value makes sure it's properly handled during the calculation.

    /sys/devices/system/memory/memory*

    Iterating over all available memory blocks to verify they are online and add the calculated block size to totalmem if they are.

    [[ "$(cat ${mem}/online)" == "1" ]] &&

    You can change or remove this if you prefer another memory state.

    0 讨论(0)
  • 2020-12-07 10:15

    In Linux Kernel, present pages are physical pages of RAM which kernel can see. Literally, present pages is total size of RAM in 4KB unit.

    grep present /proc/zoneinfo | awk '{sum+=$2}END{print sum*4,"KB"}'
    

    The 'MemTotal' form /proc/meminfo is the total size of memory managed by buddy system.And we can also compute it like this:

    grep managed /proc/zoneinfo | awk '{sum+=$2}END{print sum*4,"KB"}'
    
    0 讨论(0)
  • 2020-12-07 10:17

    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"}'
    
    0 讨论(0)
  • 2020-12-07 10:17

    Total memory in Mb:

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

    or:

    x=$(awk '/MemTotal/ {print $2}' /proc/meminfo) ; echo $((x/1024))
    
    0 讨论(0)
提交回复
热议问题