zRAM for compressing pages
mercredi 21 août 2013 à 18:57Since paging (or swapping) on disk was really slow as hell on my laptop (2GB of RAM) when I ran out of memory, I decided to use zRAM which will set up a compressed block device in RAM and use it as a swap space. And yes this can lead to better performances! Tested and approved! (Compressed ratio is approximatively 3:1).
On Ubuntu you have a package which install an upstart job handling the setup of zRAM. It will automagically set the zRAM swap space.
- Optional, remove your disk swap space (swapoff -a for temporary) in your fstab.
- Install zram-config.
- The upstart job is launched and your zRAM swap is set!
On other distributions, you can do what the Ubuntu’s job do, it basically detect the number of threads, set x threads zRAM devices and use it as a swap space with a priority of 5:
# 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=`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