解释 Zram 和 Swap 数

解释 Zram 和 Swap 数

好的,这里确实有两个问题,为什么我的磁盘交换被使用?我的交换区的压缩真的这么好吗?这看起来不太可能,但也有可能是因为应用程序(如 JVM)保留了内存,但实际上并未使用它。

Linux manjaro 5.10.59-1-MANJARO #1 SMP PREEMPT Sun Aug 15 13:11:32 UTC 2021 x86_64 GNU/Linux

这是我的初步测量结果

❯ swapon
NAME           TYPE       SIZE USED PRIO
/dev/nvme0n1p6 partition 32.2G 2.4G   -2
/dev/zram0     partition    4G 3.5G  100
❯ zramctl
NAME       ALGORITHM DISKSIZE  DATA COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram0 zstd            4G 42.4M 42.4M 42.4M       8 [SWAP]
MemTotal:       16138912 kB
MemFree:          506156 kB
MemAvailable:    1338368 kB
Buffers:              32 kB
Cached:          3733980 kB
SwapCached:      2874732 kB
Active:          6357760 kB
Inactive:        7098592 kB
Active(anon):    5746908 kB
Inactive(anon):  6665988 kB
Active(file):     610852 kB
Inactive(file):   432604 kB
Unevictable:      367084 kB
Mlocked:             224 kB
SwapTotal:      37986296 kB
SwapFree:       31745392 kB
Dirty:              2172 kB
Writeback:             0 kB
AnonPages:       7635524 kB
Mapped:           628752 kB
Shmem:           2692308 kB
KReclaimable:     104768 kB
Slab:             330300 kB
SReclaimable:     104768 kB
SUnreclaim:       225532 kB
KernelStack:       25600 kB
PageTables:        81764 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:    46055752 kB
Committed_AS:   24622488 kB
VmallocTotal:   34359738367 kB
VmallocUsed:      110656 kB
VmallocChunk:          0 kB
Percpu:             7552 kB
HardwareCorrupted:     0 kB
AnonHugePages:         0 kB
ShmemHugePages:        0 kB
ShmemPmdMapped:        0 kB
FileHugePages:         0 kB
FilePmdMapped:         0 kB
CmaTotal:              0 kB
CmaFree:               0 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
Hugetlb:               0 kB
DirectMap4k:      813792 kB
DirectMap2M:    15714304 kB
DirectMap1G:           0 kB
❯ free -h
               total        used        free      shared  buff/cache   available
Mem:            15Gi        11Gi       274Mi       2.5Gi       3.9Gi       1.4Gi
Swap:           36Gi       6.0Gi        30Gi

❯ sudo swapoff /dev/nvme0n1p6

swapoff

❯ zramctl
NAME       ALGORITHM DISKSIZE  DATA COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram0 zstd            4G 42.9M 42.9M 42.9M       8 [SWAP]
❯ swapon
NAME       TYPE      SIZE USED PRIO
/dev/zram0 partition   4G 3.5G  100
❯ cat /proc/meminfo
MemTotal:       16138912 kB
MemFree:          450240 kB
MemAvailable:    1018456 kB
Buffers:              28 kB
Cached:          3643988 kB
SwapCached:      1249912 kB
Active:          6562884 kB
Inactive:        7350540 kB
Active(anon):    5994940 kB
Inactive(anon):  7018496 kB
Active(file):     567944 kB
Inactive(file):   332044 kB
Unevictable:      168744 kB
Mlocked:             224 kB
SwapTotal:       4194300 kB
SwapFree:         115892 kB
Dirty:               696 kB
Writeback:             0 kB
AnonPages:       9229592 kB
Mapped:           477084 kB
Shmem:           2744240 kB
KReclaimable:     105376 kB
Slab:             326216 kB
SReclaimable:     105376 kB
SUnreclaim:       220840 kB
KernelStack:       26096 kB
PageTables:        83560 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:    12263756 kB
Committed_AS:   25151912 kB
VmallocTotal:   34359738367 kB
VmallocUsed:      115640 kB
VmallocChunk:          0 kB
Percpu:             7552 kB
HardwareCorrupted:     0 kB
AnonHugePages:         0 kB
ShmemHugePages:        0 kB
ShmemPmdMapped:        0 kB
FileHugePages:         0 kB
FilePmdMapped:         0 kB
CmaTotal:              0 kB
CmaFree:               0 kB
HugePages_Total:       0HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
Hugetlb:               0 kB
DirectMap4k:      821984 kB
DirectMap2M:    15706112 kB
DirectMap1G:           0 kB
❯ cat /proc/swaps
Filename                Type        Size        Used        Priority
/dev/zram0                              partition   4194300     4086744     100
❯ free -h
               total        used        free      shared  buff/cache   available
Mem:            15Gi        10Gi       1.0Gi       2.6Gi       4.1Gi       2.1Gi
Swap:          4.0Gi       3.9Gi        97Mi

那么为什么zram只有42M呢?这真的是从3.5G压缩到的吗?如果这是真的,为什么我的交换分区被使用?不应该全部都在吗zram

答案1

您的内核可能正在使用 zswap。从www.kernel.org:

Zswap 是一个轻量级的交换页压缩缓存。它获取正在换出的页面并尝试将它们压缩到动态分配的基于 RAM 的内存池中。

约 3.5GB 的数据已被“交换”,但位于压缩的 zswap 池中,并且尚未被逐出到后备交换设备 (zram),因此从zramctl.

您可以通过禁用 zswap 并通过删除并重新添加 zram 交换设备强制其清空池来确认这一点:

# echo 0 > /sys/module/zswap/parameters/enabled
# swapoff /dev/zram0
# swapon /dev/zram0

相关内容