小ネタ: 搭載メモリ総量はどのコマンドで確認するべきなのか?


はじめに

IBM CloudのVPCでサーバーのプロファイルを見てみると、GB(ギガバイト: $10^{9}$)ではなくGiB(ギビバイト: $2^{30}$)表示になっている。

「ほうほう」と思って、16GiBのCentOSを注文してメモリを(定番の)freeコマンドを使って見てみたら、メモリ総量が16GiB(=16384MiB)もないことに気がついた。

検証結果

free

[root@syasuda16gb ~]# free -g
              total        used        free      shared  buff/cache   available
Mem:             15           0          15           0           0          15
Swap:             0           0           0

[root@syasuda16gb ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:          16047         177       15746          16         123       15634
Swap:             0           0           0

[root@syasuda16gb ~]# free -k
              total        used        free      shared  buff/cache   available
Mem:       16433128      183160    16122872       16816      127096    16008688
Swap:            40           0          40

[root@syasuda16gb ~]# free -b
              total        used        free      shared  buff/cache   available
Mem:    16827523072   186245120 16511647744    17219584   129630208 16394469376
Swap:         40960           0       40960

$16827523072/1024/1024=16047$ なので、freeの結果が二進法で計算されているのは間違いなさそう。

/proc/meminfo

MemTotalの項を見た感じでは、16433128 kBでありfreeの結果と同じである。

[root@syasuda16gb ~]# cat /proc/meminfo
MemTotal:       16433128 kB
MemFree:        16122476 kB
MemAvailable:   16008292 kB
Buffers:           20912 kB
Cached:            88824 kB
SwapCached:            0 kB
Active:            67112 kB
Inactive:          81664 kB
Active(anon):      39236 kB
Inactive(anon):    16616 kB
Active(file):      27876 kB
Inactive(file):    65048 kB
Unevictable:           0 kB
Mlocked:               0 kB
SwapTotal:            40 kB
SwapFree:             40 kB
Dirty:                60 kB
Writeback:             0 kB
AnonPages:         39072 kB
Mapped:            23580 kB
Shmem:             16816 kB
Slab:              42144 kB
SReclaimable:      17356 kB
SUnreclaim:        24788 kB
KernelStack:        1728 kB
PageTables:         3240 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:     8216604 kB
Committed_AS:     185352 kB
VmallocTotal:   34359738367 kB
VmallocUsed:       30312 kB
VmallocChunk:   34359705664 kB
HardwareCorrupted:     0 kB
AnonHugePages:      8192 kB
CmaTotal:              0 kB
CmaFree:               0 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
DirectMap4k:      110432 kB
DirectMap2M:     5136384 kB
DirectMap1G:    12582912 kB

top

KiB Memの項を見た感じでは、16433128でありfreeの結果と同じである。

[root@syasuda16gb ~]# top
top - 07:21:13 up 15:09,  1 user,  load average: 0.00, 0.01, 0.03
Tasks:  91 total,   2 running,  89 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.1 sy,  0.0 ni, 99.9 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem : 16433128 total, 16121512 free,   184168 used,   127448 buff/cache
KiB Swap:       40 total,       40 free,        0 used. 16007508 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
    1 root      20   0   45988   6472   4136 S   0.0  0.0   0:03.24 systemd
    2 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kthreadd
    4 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/0:0H
    5 root      20   0       0      0      0 S   0.0  0.0   0:00.34 kworker/u8:0
    6 root      20   0       0      0      0 S   0.0  0.0   0:00.00 ksoftirqd/0
    7 root      rt   0       0      0      0 S   0.0  0.0   0:00.01 migration/0
    8 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcu_bh
    9 root      20   0       0      0      0 R   0.0  0.0   0:00.55 rcu_sched
   10 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 lru-add-drain
   11 root      rt   0       0      0      0 S   0.0  0.0   0:00.20 watchdog/0
   12 root      rt   0       0      0      0 S   0.0  0.0   0:00.12 watchdog/1
   13 root      rt   0       0      0      0 S   0.0  0.0   0:00.05 migration/1
   14 root      20   0       0      0      0 S   0.0  0.0   0:00.13 ksoftirqd/1
   16 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/1:0H
   17 root      rt   0       0      0      0 S   0.0  0.0   0:00.14 watchdog/2
   18 root      rt   0       0      0      0 S   0.0  0.0   0:00.01 migration/2
   19 root      20   0       0      0      0 S   0.0  0.0   0:00.01 ksoftirqd/2
   21 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/2:0H

結論

この答えは

などの記事に書いてあった。要はKernel自身がメモリ管理のために一定サイズのメモリ量を予約するため、その予約したメモリ量を差し引いて総量としてfree/topなどのコマンドは表示しているようだ(これは仕様でありバグではない)。

じゃあ、どうやったら本当のメモリ量を確認できるのかというと、dmidecodeというハードウェアの情報を取得するコマンドで確認できる。確かに以下の結果からは、Size: 16384 MBと認識されている。

[root@syasuda16gb ~]# dmidecode -t memory
# dmidecode 3.2
Getting SMBIOS data from sysfs.
SMBIOS 2.8 present.

Handle 0x1000, DMI type 16, 23 bytes
Physical Memory Array
    Location: Other
    Use: System Memory
    Error Correction Type: Multi-bit ECC
    Maximum Capacity: 16388 MB
    Error Information Handle: Not Provided
    Number Of Devices: 2

Handle 0x1100, DMI type 17, 40 bytes
Memory Device
    Array Handle: 0x1000
    Error Information Handle: Not Provided
    Total Width: Unknown
    Data Width: Unknown
    Size: 16384 MB
    Form Factor: DIMM
    Set: None
    Locator: DIMM 0
    Bank Locator: Not Specified
    Type: RAM
    Type Detail: Other
    Speed: Unknown
    Manufacturer: QEMU
    Serial Number: Not Specified
    Asset Tag: Not Specified
    Part Number: Not Specified
    Rank: Unknown
    Configured Memory Speed: Unknown
    Minimum Voltage: Unknown
    Maximum Voltage: Unknown
    Configured Voltage: Unknown

Handle 0x1101, DMI type 17, 40 bytes
Memory Device
    Array Handle: 0x1000
    Error Information Handle: Not Provided
    Total Width: Unknown
    Data Width: Unknown
    Size: 4 MB
    Form Factor: DIMM
    Set: None
    Locator: DIMM 1
    Bank Locator: Not Specified
    Type: RAM
    Type Detail: Other
    Speed: Unknown
    Manufacturer: QEMU
    Serial Number: Not Specified
    Asset Tag: Not Specified
    Part Number: Not Specified
    Rank: Unknown
    Configured Memory Speed: Unknown
    Minimum Voltage: Unknown
    Maximum Voltage: Unknown
    Configured Voltage: Unknown