最後更新: 2017-10-25
介紹
zRam 它的前身是 compcache 來, 現在已經由 Kernel 完全實現
用意是在 Ram 上建立一個 compressed block device
之後把原本 swap 到 HardDisk 上的東西 swap 到它上面先
如果系統真的有需要的話, 那才 swap 到慢慢的 HardDisk 上
* This reduces the need to read and write memory swap files on disk
自動建立 zram
apt-get install zram-config
安裝後會有以下啟動檔
- /etc/init/zram-config.conf
start on runlevel [2345] pre-start exec /usr/bin/init-zram-swapping pre-stop exec /usr/bin/end-zram-swapping
/usr/bin/init-zram-swapping
它主要有以下 3 個工作(過程)
# load dependency modules # 當有兩個 core 時, 那會有兩個 zram NRDEVICES=$(grep -c ^processor /proc/cpuinfo | sed 's/^0$/1/') modprobe zram $MODPROBE_ARGS # initialize the devices # 用 1/2 Ram 來做 zRam !! # 單位 byte totalmem=`free | grep -e "^Mem:" | sed -e 's/^Mem: *//' -e 's/ *.*//'` mem=$(((totalmem / 2 / ${NRDEVICES}) * 1024)) echo $mem > /sys/block/zram${DEVNUMBER}/disksize # Creating swap filesystems mkswap /dev/zram${DEVNUMBER}
啟動前:
root@home:~# free -m total used free shared buffers cached Mem: 3133 2879 254 0 125 1128 -/+ buffers/cache: 1624 1509 Swap: 1906 88 1818
啟動後會見到 zram device
root@home:~# ls -al /dev/zram*
brw-rw---- 1 root disk 251, 0 Dec 26 00:57 /dev/zram0 brw-rw---- 1 root disk 251, 1 Dec 26 00:57 /dev/zram1
# 系統的 swap 會多了, 不過 ram 是不會少了
root@home:~# free -m total used free shared buffers cached Mem: 3133 2904 229 0 125 1127 -/+ buffers/cache: 1652 1481 Swap: 3473 88 3385
# 24G Ram 除 4 後的效果
cat /proc/swaps
Filename Type Size Used Priority /dev/sda2 partition 5859324 0 -1 /dev/zram0 partition 3068308 848 5 /dev/zram1 partition 3068308 848 5 /dev/zram2 partition 3068308 860 5 /dev/zram3 partition 3068308 844 5
# Disable auto start
systemctl disable zram-config
人手建立 zRam
Start:
modprobe zram
# 196MB
echo 205520896 > /sys/block/zram0/disksize
mkswap /dev/zram0
swapon -p 10 /dev/zram0
Stop:
swapoff /dev/zram0
rmmod zram
Remark
# This creates 4 devices: /dev/zram{0,1,2,3}
modprobe zram num_devices=4
zram 對 vps 的影響
在 2 號啟用 zram 後, vps host server 的 cpu usage 明顯下降了 !!
More about zram devices
/sys/block/zram<id>/
mem_used_max # reset the `mem_used_max' counter
compact # trigger memory compaction
reset # trigger device reset
# This frees all the memory allocated for the given device and resets the disksize to zero.
Statistics
mm_stat
cat /sys/block/zram0/mm_stat
111964160 21987017 23433216 0 23433216 7523 0 955
orig_data_size # uncompressed size of data stored in this disk.
compr_data_size # compressed size of data stored in this disk
mem_used_total # the amount of memory allocated for this disk.
(includes allocator fragmentation and metadata overhead)
---
mem_limit # the maximum amount of memory ZRAM can use to store the compressed data
---
mem_used_max # the maximum amount of memory zram have consumed to store the data
---
same_pages # the number of same element filled pages written to this disk.
# No memory is allocated for such pages.
pages_compacted the number of pages freed during compaction
huge_pages the number of incompressible pages
io_stat
cat /sys/block/zram0/io_stat
0 0 0 5030292
- failed_reads
- failed_writes
- invalid_io
- notify_free ?
bd_stat
cat /sys/block/zram0/bd_stat
0 0 0
bd_count size of data written in backing device. Unit: 4K bytes
bd_reads the number of reads from backing device Unit: 4K bytes
bd_writes the number of writes to backing device Unit: 4K bytes
zramctl
util-linux 提供了 zramctl 去管理 zram
# Version & Help
zramctl -V
zramctl from util-linux 2.36.1
zramctl -h
# List setting
# 未被 mkswap 及 swapon 的 zram device
zramctl
NAME ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT /dev/zram0 lz4 2G 0B 0B 0B 4
Remark
- DATA = orig_data_size
- COMPR = compr_data_size
- TOTAL = mem_used_total
# Reset the options of the specified zram device(s) <= 相當於刪除了那 device
-r, --reset
zramctl -r /dev/zram0
# Set zram disk size. Unit: KB (=1000), MB (=1000*1000)
-s, --size size
i.e.
zramctl -s 230g zram0
# Set the compression algorithm
# lzo|lz4|lz4hc|deflate|842, Default: lz4
# 不能獨立使用, 要配以 -s
-a, --algorithm
zramctl -a lz4 -s 2g /dev/zram0
# Set the maximum number of compression streams that can be used for the device.
# Default: All CPU Core
-t, --streams number
# 找出下一個未被使用的 zram device (zram0, zram1 ...)
-f, --find # Find the first unused zram device.
# 與 -s 一同使用時, 會 initialize the device
i.e.
zramctl -f
/dev/zram0
Usage Example
zramctl -f -a lz4 -s 2g
/dev/zram0
mkswap /dev/zram0
swapon -p 1 /dev/zram0
zramctl
NAME ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT /dev/zram0 lz4 2G 4K 64B 4K 4 [SWAP]
swapon
NAME TYPE SIZE USED PRIO /dev/sdd6 partition 7.8G 77.7M -2 /dev/zram0 partition 2G 512K 1
Streams & Algorithm
# number of compression streams. Default: one per online CPUs
cat /sys/block/zram0/max_comp_strea
# compression algorithm
cat /sys/block/zram0/comp_algorithm
lzo lzo-rle [lz4] lz4hc 842 zstd
lz4hc vs lz4
HC = higher compression
Windows 10
RAM compression is handled by a process known as Superfetch.
keep in mind that compression only runs when it detects that there are idle apps stored in memory.
Hard Faults: Also known as Page Faults.
Commit: The amount of space the application has needed in the paging file.
Shareable: The amount of the Working Set that can be or is being shared with other processes.
This is also an indicator of RAM that can be given up to other processes.
發現 memory leak
Task Manager -> open applications -> wait it for an hour or two.
Physical Memory Usage History graph continually rises => memory leak