许吉友 - 运维

内存也是 Linux 性能分析中至关重要的一环。下面先来看下怎么查看内存信息。

/proc/meminfo

这个文件中记录了内存的各项信息。

$ cat /proc/meminfo
MemTotal:       16323904 kB
MemFree:        16033848 kB
MemAvailable:   15923900 kB
Buffers:            2092 kB
Cached:           103156 kB
SwapCached:            0 kB
Active:            95692 kB
Inactive:          68888 kB
Active(anon):      59840 kB
Inactive(anon):     9096 kB
Active(file):      35852 kB
Inactive(file):    59792 kB
Unevictable:           0 kB
Mlocked:               0 kB
SwapTotal:       2096124 kB
SwapFree:        2096124 kB
Dirty:                16 kB
Writeback:             0 kB
AnonPages:         59364 kB
Mapped:            45988 kB
Shmem:              9604 kB
Slab:              42684 kB
SReclaimable:      19028 kB
SUnreclaim:        23656 kB
KernelStack:        3392 kB
PageTables:         5360 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:    10258076 kB
Committed_AS:     719480 kB
VmallocTotal:   34359738367 kB
VmallocUsed:           0 kB
VmallocChunk:          0 kB
HardwareCorrupted:     0 kB
AnonHugePages:     22528 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
DirectMap4k:       86388 kB
DirectMap2M:     1902592 kB
DirectMap1G:    15728640 kB

free

很常用的一个命令:

$ free -h

MemTotal = used + free + buff/cache(单位 K)

shared 字段,这个是多进程的共享内存空间,不常用

有时候 free 很小,buff/cache 却很大,这是 Linux 的内存设计决定的,Linux 的想法是内存闲着反正也是闲着,不如拿出来做系统缓存和缓冲区,提高数据读写的速率。但是当系统内存不足时,buff/cache 会让出部分来,非常灵活的操作。

dmidecode

对于内存,可以使用以下命令查看:

$ dmidecode -t memory
Getting SMBIOS data from sysfs.
SMBIOS 2.7 present.

Handle 0x0007, DMI type 16, 23 bytes
Physical Memory Array
        Location: System Board Or Motherboard
        Use: System Memory
        Error Correction Type: None
        Maximum Capacity: 32 GB
        Error Information Handle: Not Provided
        Number Of Devices: 4

Handle 0x0042, DMI type 17, 34 bytes
Memory Device
        Array Handle: 0x0007
        Error Information Handle: Not Provided
        Total Width: 64 bits
        Data Width: 64 bits
        Size: 8192 MB
        Form Factor: DIMM
        Set: None
        Locator: ChannelA-DIMM0
        Bank Locator: BANK 0
        Type: DDR3
        Type Detail: Synchronous
        Speed: 1600 MT/s
        Manufacturer: Kingston
        Serial Number: 9F20B925
        Asset Tag: 9876543210
        Part Number: 99P5471-041.A00LF 
        Rank: 2
        Configured Memory Speed: 1600 MT/s

Handle 0x0044, DMI type 17, 34 bytes
Memory Device
        Array Handle: 0x0007
        Error Information Handle: Not Provided
        Total Width: 64 bits
        Data Width: 64 bits
        Size: 8192 MB
        Form Factor: DIMM
        Set: None
        Locator: ChannelA-DIMM1
        Bank Locator: BANK 1
        Type: DDR3
        Type Detail: Synchronous
        Speed: 1600 MT/s
        Manufacturer: Kingston
        Serial Number: 9D203226
        Asset Tag: 9876543210
        Part Number: 99P5471-041.A00LF 
        Rank: 2
        Configured Memory Speed: 1600 MT/s

Handle 0x0046, DMI type 17, 34 bytes
Memory Device
        Array Handle: 0x0007
        Error Information Handle: Not Provided
        Total Width: Unknown
        Data Width: Unknown
        Size: No Module Installed
        Form Factor: DIMM
        Set: None
        Locator: ChannelB-DIMM0
        Bank Locator: BANK 2
        Type: Unknown
        Type Detail: None
        Speed: Unknown
        Manufacturer: [Empty]
        Serial Number: [Empty]
        Asset Tag: 9876543210
        Part Number: [Empty]
        Rank: Unknown
        Configured Memory Speed: Unknown

Handle 0x0047, DMI type 17, 34 bytes
Memory Device
        Array Handle: 0x0007
        Error Information Handle: Not Provided
        Total Width: Unknown
        Data Width: Unknown
        Size: No Module Installed
        Form Factor: DIMM
        Set: None
        Locator: ChannelB-DIMM1
        Bank Locator: BANK 3
        Type: Unknown
        Type Detail: None
        Speed: Unknown
        Manufacturer: [Empty]
        Serial Number: [Empty]
        Asset Tag: 9876543210
        Part Number: [Empty]
        Rank: Unknown
        Configured Memory Speed: Unknown

从上面看出,我这台 PC 有两块内存卡,一块 8 G,共 16 G。