Linux-Kernel

解釋 Zram 和交換號碼

  • April 16, 2022

好的,所以這裡真的有兩個問題,為什麼要使用我的磁碟交換?我的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嗎?

您的核心很可能正在使用 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

引用自:https://unix.stackexchange.com/questions/666307