虚拟机中的“错误”总内存

虚拟机中的“错误”总内存

我在 VMware Fusion 10.1.1(在 High Sierra 中)中运行了几个 Debian 9.3 虚拟机,其内核为 4.9.0-5-amd64。

我担心的是,Free 中的总内存不能正确反映我为多个虚拟机留出的内存。

作为测试,我为虚拟机预留了 2GB 空间,但free -m仅显示 1986MB:

$ free -m
              total        used        free      shared  buff/cache   available
Mem:           1986          51        1864           1          70        1825

或没有-m

$ free 
              total        used        free      shared  buff/cache   available
Mem:        2033760       52264     1909584        1108       71912     1869628
Swap:        999420           0      999420

调试情况我也查看/proc/meminfo并发现了这一点:

$ egrep "MemTotal|DirectMap2M" /proc/meminfo
MemTotal:        2033760 kB
DirectMap2M:     2054144 kB

所以实际上 DirectMap2M 反映的是 2GB;为什么 MemTotal 大约有。那么少了20MB?

有趣的是,在谷歌搜索 MemTotal/DirectMap2M 时发现了这篇文章:内存/Ram 错误

如果您的操作系统通过 free –mtop命令显示错误的 RAM 分配,请确保您确实为 VPS 分配了正确的 RAM 量。

这只是错误的报告,这是因为我们使用最新的 Xen 4.xx 版本来增强性能,不幸的是,这可能会导致这种异常,在 32 位操作系统模板上更是如此。

另请记住,更现代的内核不会向操作系统提供一些内核保留内存。

那么这里发生了什么?是内核按照他们的建议保留内存吗?为了什么目的?
(是的,他们正在谈论 Xen,这是关于 VmWare Fusion,但这可能是一个线索)

用于补充:

vmstat 输出:

$ vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0      0 1909344  13988  57928    0    0    31     0   19   37  0  0 100  0  0

顶部输出:

$ top -b -n 1 | grep Mem
KiB Mem :  2033760 total,  1906228 free,    52836 used,    74696 buff/cache
KiB Swap:   999420 total,   999420 free,        0 used.  1867664 avail Mem 

dmidecode输出(部分);

Handle 0x0085, DMI type 6, 12 bytes
Memory Module Information
        Socket Designation: RAM socket #0
        Bank Connections: None
        Current Speed: Unknown
        Type: EDO DIMM
        Installed Size: 2048 MB (Single-bank Connection)
        Enabled Size: 2048 MB (Single-bank Connection)
        Error Status: OK

输出dmesg

$ sudo dmesg | egrep "Memory|Free|ACPI" | egrep -v "edge|wakeup|noapic|Added|Bug|IRQ|pnp|Plug"
[    0.000000] BIOS-e820: [mem 0x000000007fee0000-0x000000007fefefff] ACPI data
[    0.000000] BIOS-e820: [mem 0x000000007feff000-0x000000007fefffff] ACPI NVS
[    0.000000] ACPI: Early table checksum verification disabled
[    0.000000] ACPI: RSDP 0x00000000000F6A10 000024 (v02 PTLTD )
[    0.000000] ACPI: XSDT 0x000000007FEEB683 00005C (v01 INTEL  440BX    06040000 VMW  01324272)
[    0.000000] ACPI: FACP 0x000000007FEFEE73 0000F4 (v04 INTEL  440BX    06040000 PTL  000F4240)
[    0.000000] ACPI: DSDT 0x000000007FEEC923 012550 (v01 PTLTD  Custom   06040000 MSFT 03000001)
[    0.000000] ACPI: FACS 0x000000007FEFFFC0 000040
[    0.000000] ACPI: FACS 0x000000007FEFFFC0 000040
[    0.000000] ACPI: BOOT 0x000000007FEEC8FB 000028 (v01 PTLTD  $SBFTBL$ 06040000  LTP 00000001)
[    0.000000] ACPI: APIC 0x000000007FEEC1B9 000742 (v01 PTLTD  ? APIC   06040000  LTP 00000000)
[    0.000000] ACPI: MCFG 0x000000007FEEC17D 00003C (v01 PTLTD  $PCITBL$ 06040000  LTP 00000001)
[    0.000000] ACPI: SRAT 0x000000007FEEB77F 000880 (v02 VMWARE MEMPLUG  06040000 VMW  00000001)
[    0.000000] ACPI: HPET 0x000000007FEEB747 000038 (v01 VMWARE VMW HPET 06040000 VMW  00000001)
[    0.000000] ACPI: WAET 0x000000007FEEB71F 000028 (v01 VMWARE VMW WAET 06040000 VMW  00000001)
[    0.000000] ACPI: Local APIC address 0xfee00000
[    0.000000] ACPI: SRAT: Node 0 PXM 0 [mem 0x00000000-0x0009ffff]
[    0.000000] ACPI: SRAT: Node 0 PXM 0 [mem 0x00100000-0x7fffffff]
[    0.000000] ACPI: PM-Timer IO Port: 0x1008
[    0.000000] ACPI: Local APIC address 0xfee00000
[    0.000000] Using ACPI for processor (LAPIC) configuration information
[    0.000000] ACPI: HPET id: 0x8086af01 base: 0xfed00000
[    0.000000] Memory: 2011544K/2096628K available (6196K kernel code, 1159K rwdata, 2848K rodata, 1408K init, 688K bss, 85084K reserved, 0K cma-reserved)
[    0.005465] ACPI: 1 ACPI AML tables successfully acquired and loaded
[    0.005475] ACPI: setting ELCR to 0200 (from 0e80)
[    0.044362] x86/mm: Memory block size: 128MB
[    0.046853] PM: Registering ACPI NVS region [mem 0x7feff000-0x7fefffff] (4096 bytes)
[    0.170037] ACPI: PCI Root Bridge [PCI0] (domain 0000 [bus 00-7f])
[    0.178855] pci 0000:00:07.3: quirk: [io  0x1000-0x103f] claimed by PIIX4 ACPI
[    0.282772] ACPI: Enabled 2 GPEs in block 00 to 0F
[    0.995687] Freeing initrd memory: 17572K
[    1.085789] Freeing unused kernel memory: 1408K
[    1.085832] Freeing unused kernel memory: 1980K
[    1.085873] Freeing unused kernel memory: 1248K

完整/proc/meminfo输出:

$ cat /proc/meminfo 
MemTotal:        2033760 kB
MemFree:         1906864 kB
MemAvailable:    1868300 kB
Buffers:           14720 kB
Cached:            50220 kB
SwapCached:            0 kB
Active:            45004 kB
Inactive:          28204 kB
Active(anon):       8308 kB
Inactive(anon):     1064 kB
Active(file):      36696 kB
Inactive(file):    27140 kB
Unevictable:           0 kB
Mlocked:               0 kB
SwapTotal:        999420 kB
SwapFree:         999420 kB
Dirty:                 0 kB
Writeback:             0 kB
AnonPages:          8284 kB
Mapped:            14860 kB
Shmem:              1108 kB
Slab:              23996 kB
SReclaimable:       9756 kB
SUnreclaim:        14240 kB
KernelStack:        3052 kB
PageTables:         1348 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:     2016300 kB
Committed_AS:      38852 kB
VmallocTotal:   34359738367 kB
VmallocUsed:           0 kB
VmallocChunk:          0 kB
HardwareCorrupted:     0 kB
AnonHugePages:         0 kB
ShmemHugePages:        0 kB
ShmemPmdMapped:        0 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
DirectMap4k:       42880 kB
DirectMap2M:     2054144 kB
DirectMap1G:           0 kB

答案1

free/proc/meminfo仅显示用户空间实际可用的内存;内核留出一些内存供自己使用。如果您Memory:在启动日志中查找一行(/var/log/dmesg.0或类似的行,或journalctl),您会看到类似的内容

Memory: 32818828K/33439808K available (5612K kernel code, 1083K rwdata, 1896K rodata, 1264K init, 832K bss, 620980K reserved, 0K cma-reserved)

启动后可用的内存量通常会略大于此处指示的量,因为一些用于初始化的内存会返回给系统,并且保留的内存量可能会发生变化(例如如果它是为集成 GPU 保留的);就我而言,MemTotal显示 32062 MiB 而不是上面给出的 32049 MiB。

在您的情况下,仅保留 62MiB (2048 – 1986);这足以覆盖内核代码和数据,再加上一些保留的内存。启动日志还将包括系统内存映射的详细信息,该映射应占大部分保留内存(它是为固件、ACPI 等保留的,即使在虚拟机中也是如此)。

MemTotal永远不会与已安装的物理内存量或为虚拟机分配的内存量相对应,并且这很正常

答案2

补充@StephenKitt的答案,确认MemTotal不是整个可用RAM的值:

从内核源代码来看,Documentation/filesystems/proc.txt

MemTotal - 可用内存总量(即物理内存减去一些保留位和内核二进制代码)

同样在启动时,x86/boot/compressed/kaslr.c

enum mem_avoid_index {
    MEM_AVOID_ZO_RANGE = 0,
    MEM_AVOID_INITRD,
    MEM_AVOID_CMDLINE,
    MEM_AVOID_BOOTPARAMS,
    MEM_AVOID_MEMMAP_BEGIN,
    MEM_AVOID_MEMMAP_END = MEM_AVOID_MEMMAP_BEGIN + MAX_MEMMAP_REGIONS - 1,
    MEM_AVOID_MAX,
};

相关内容