想象一下这样的场景:2GiB 被交换为 zram 并压缩到 1GiB。
一旦内存压力减轻并且2GiB逐渐变为不可交换,Linux是否会释放用于存储压缩zram页面的1GiB页面?
如果是这样,它是否会对现有页面进行碎片整理?
压缩页面中必须有多个页面,当除一页之外的所有页面都未交换时会发生什么?所有页面都保留在内存中,直到最后一页也被释放吗?
答案1
我会默认说不,但可以这样做。
$ lsblk --discard /dev/zram0
NAME DISC-ALN DISC-GRAN DISC-MAX DISC-ZERO
zram0 0 4K 2T 0
这意味着zram0
是一个可丢弃的设备。
-d
,--discard[=policy]
启用交换丢弃,交换支持设备是否支持丢弃或修剪操作。
[...]
挂载/etc/fstab
选项discard
、discard=once
、 或discard=pages
也可用于启用丢弃标志。
人们只需弄清楚在特定的 Linux 发行版上swapon
执行命令或等效命令的位置,并相应地添加交换 --discard
选项或山 discard
这个地方的选项(或至少是=pages
变体)。
答案2
简短回答:是的,zram 后台页面会自动释放。
经过实验检查(内核 5.10.105),似乎未使用的 zram 存储会自动释放,即使 zram 设备在没有discard
.
概括:下面的脚本运行一个分配大块内存的进程。
zram 使用量(通过 检查zramctl
)最初会增加,然后在停止进程并逐出交换页面后回到基线。
# * I've run this on a freshly booted VM *
# zram is mounted with nodiscard to exclude any effects of
# `discard`.
sudo grep zram /etc/fstab
# /dev/zram0 none swap nodiscard,pri=5
# We have ~6 GiB of RAM
grep -i memtotal /proc/meminfo
# MemTotal: 6386852 kB
# Show zram usage.
# `DATA` is the total amount of uncompressed data currently stored in zram.
zramctl
# NAME ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT
# /dev/zram0 zstd 3.1G 4K 58B 4K 4 [SWAP]
# Start a process that allocates 10 GiB of RAM
stress-ng -- --vm-bytes $((10*1024**3)) --vm-keep --vm 1 &
# *Wait some time for the stress test command to be swapped out*
# zram usage has gone up from 4 KiB to 3.1 GiB
zramctl
# NAME ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT
# /dev/zram0 zstd 3.1G 3.1G 1.1G 1.2G 4 [SWAP]
# Stop stress test
kill %1
# zram usage decreased from 3.1 GiB to 0.3 GiB
zramctl
# NAME ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT
# /dev/zram0 zstd 3.1G 336.8M 48.6M 57.6M 4 [SWAP]
# Read the first byte of all memory pages of all processes.
# This evicts all non-kernel swapped pages without using `swapoff`, which might
# reset the zram device.
sudo ./read_all_mem_pages.rb
# Now zram usage is almost back to zero
zramctl
# NAME ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT
# /dev/zram0 zstd 3.1G 18.4M 3.5M 5.9M 4 [SWAP]
来源read_all_mem_pages.rb
:
#!/usr/bin/env ruby
def access_all_pages(pid)
name = File.basename(File.readlink("/proc/#{pid}/exe")) rescue return
puts "#{pid} (#{name})"
File.open("/proc/#{pid}/mem", 'r') do |mem|
for_each_mem_page(pid) do |page_address|
mem.seek(page_address)
mem.read(1) rescue nil
end
end
end
def for_each_mem_page(pid)
File.foreach("/proc/#{pid}/maps") do |line|
fields = line.split
range, dest = fields[0], fields[-1]
next if dest == "[vsyscall]"
start, end_ = range.split('-').map { |x| x.to_i(16) }
address = start
while address < end_
yield address
address += 4096
end
end
end
pids = Dir.children('/proc').grep(/^\d+$/).map(&:to_i)
pids.each { |pid| access_all_pages(pid) }
答案3
我相信 zRam 消耗的内存永远不会被释放。看
sudo zramctl --output-all
NAME DISKSIZE DATA COMPR ALGORITHM STREAMS ZERO-PAGES TOTAL MEM-LIMIT MEM-USED MIGRATED MOUNTPOINT
/dev/zram0 5,4G 56,6M 26,5M lz4 2 697 28,2M 0B 28,2M 0B [SWAP]
重要的不是“TOTAL”的增加和减少,而是“MEM-USED”,据我测试,它永远不会减少。 (我必须重新启动 zramswap.service 才能将其恢复为零)。您只能使用“--output-all”开关从 zramctl 获取“MEM-USED”字段。