zram设置

linux 3.15之后添加了个和mac一样的功能,内存压缩zram, 之前还有个zswap, zram通常是以模块的方式被放在系统里面的,以便在可以根据需求的不同,定制化加载.

raspberry pi3的1g内存容量还是太小了,设定了zram可以挤点内存出来用, android4.4的数据貌似是40%.

设定代码:
#!/bin/sh

# load dependency modules
NRDEVICES=$(grep -c ^processor /proc/cpuinfo | sed ‘s/^0$/1/’)
if modinfo zram | grep -q ‘ zram_num_devices:’ 2>/dev/null; then
MODPROBE_ARGS=”zram_num_devices=${NRDEVICES}”
elif modinfo zram | grep -q ‘ num_devices:’ 2>/dev/null; then
MODPROBE_ARGS=”num_devices=${NRDEVICES}”
else
exit 1
fi
modprobe zram $MODPROBE_ARGS

# Calculate memory to use for zram (1/2 of ram)
totalmem=`LC_ALL=C free | grep -e “^Mem:” | sed -e ‘s/^Mem: *//’ -e ‘s/ *.*//’`
mem=$(((totalmem / 2 / ${NRDEVICES}) * 1024))

# initialize the devices
for i in $(seq ${NRDEVICES}); do
DEVNUMBER=$((i – 1))
echo $mem > /sys/block/zram${DEVNUMBER}/disksize
mkswap /dev/zram${DEVNUMBER}
swapon -p 5 /dev/zram${DEVNUMBER}
done

#关闭代码

#!/bin/sh

if DEVICES=$(grep zram /proc/swaps | awk ‘{print $1}’); then
for i in $DEVICES; do
swapoff $i
done
fi
rmmod zram

复制代码

想要开机就设定的话,只要把设定的代码添加到/etc/rc.local,开机自动设定

启动与否可以使用下面的命令查看:

swapon -s

输出结果如下

Filename Type Size Used Priority
/dev/zram0 partition 118464 0 5
/dev/zram1 partition 118464 0 5
/dev/zram2 partition 118464 0 5
/dev/zram3 partition 118464 0 5

可以看到分了4块和processor的数量是相同, 总数是总可用的物理内存的一半,至于为啥和processor数一样会比较好