1

We have a Linux machine with 32G. We capture the mem as follows:

mem=` cat /proc/meminfo | grep MemTotal | awk '{print $2}' `
echo $mem
32767184

and now we convert it to GIGA:

mem_in_giga=`  echo $(( $mem / 1024 / 1024)) `
echo $mem_in_giga
31

but from the results we get 31 and not 32G.

The same story with the free command:

free -g
              total        used        free      shared  buff/cache   available
Mem:             31           9          17           0           4          20
Swap:             7           0           7

So how do we get "32G" from any command?

PhilR
  • 149
yael
  • 13,106
  • 1
    You don't have a machine with 32 GB RAM. You have machine with approximately 32 GB RAM. If you really want 32: echo $((32767184/1000/1000)). – muru Feb 01 '18 at 12:39
  • 1
    Also, it's unclear what numbers are GB and what number may be GiB. – Kusalananda Feb 01 '18 at 12:47
  • 1
    free -g outputs gibibytes, all the values quoted from output in the question are gibibytes. That’s appropriate since RAM capacities are bought in gibibyte or tebibyte increments... – Stephen Kitt Feb 01 '18 at 12:53
  • 1
    Just FYI, you can get rid of the cat, grep, and echo: awk '$1 == "MemTotal:" { print $2 / 1024 / 1024 }' /proc/meminfo – phemmer Feb 01 '18 at 13:20
  • 1
    For future reference on converting byte values, see https://unix.stackexchange.com/q/44040/85039 – Sergiy Kolodyazhnyy Feb 01 '18 at 15:52

1 Answers1

9

MemTotal shows

Total usable RAM (i.e., physical RAM minus a few reserved bits and the kernel binary code).

You can’t use that to determine the exact installed memory, except by using heuristics...

To determine the actual installed memory, you should use lshw or dmidecode which will show the size of the installed modules; e.g. from lshw:

 *-memory
      description: System Memory
      physical id: 4c
      slot: System board or motherboard
      size: 32GiB
      capabilities: ecc
      configuration: errordetection=ecc

or in more compact form (lshw -class memory -short):

H/W path           Device      Class          Description
=========================================================
/0/0                           memory         64KiB BIOS
/0/47/48                       memory         256KiB L1 cache
/0/47/49                       memory         1MiB L2 cache
/0/47/4a                       memory         8MiB L3 cache
/0/4c                          memory         32GiB System Memory
/0/4c/0                        memory         8GiB DIMM DDR3 Synchronous 1600 MHz (0.6 ns)
/0/4c/1                        memory         8GiB DIMM DDR3 Synchronous 1600 MHz (0.6 ns)
/0/4c/2                        memory         8GiB DIMM DDR3 Synchronous 1600 MHz (0.6 ns)
/0/4c/3                        memory         8GiB DIMM DDR3 Synchronous 1600 MHz (0.6 ns)
ilkkachu
  • 138,973
Stephen Kitt
  • 434,908