Openbox - ZRAM - What it is & How to use it

by Darrel Johnston (djohnston)

ZRAM (formerly ramzswap) is a block device which is created in your computer’s memory, or RAM. It looks and acts to the system like a disk drive. However, this disk drive has only one function, to act as a swap disk. One difference between ZRAM and a normal swap partition is that the pages swapped to ZRAM are compressed before being stored. This technique has two advantages. (1) More page data can be stored because the data is compressed. (2) Because the swapped memory is stored in RAM, rather than a normal disk drive, read and write access times are much quicker.

The author of the program, nitingupta910, mentions another possible use on his Google code webpage. “With compcache at hypervisor level, we can compress any part of guest memory transparently - this is true for any type of Guest OS (Linux, Windows etc.). This should allow running more number of VMs for given amount of total host memory.”

Melodie has the older version (ramzswap) enabled on the live CDs for Openbox Bonsai and Openbox full, “out of the box” (pun intended). She has the new version (ZRAM) enabled on the Openbox Edu live CD. Both versions initialize a block device in RAM from the /etc/rc.d/rc.local file. The first (ramzswap) version creates the device /dev/ramzswap0. The size of the ramzswap0 device is defined by using the rzscontrol executable. The default size is 15% of RAM. The device can be used as a front end to a swap partition on a disk drive by defining the swap partition in the rc.local file as shown below.

# Ramzswap will act like a swap front end if RAMZ_BACKING_SWAP is defined.
# Writes are forwarded to this device when memory limit is reached or data
# is not compressible.
# i.e. RAMZ_BACKING_SWAP="/dev/sda3"

For kernel version 2.6.37 or higher, the device created is /dev/zram0. The rzscontrol executable can not be used to modify the zram0 device, so is no longer needed. Since it uses a different kernel module, the older /etc/rc.d/rc.local file must be replaced with the newer version. You can obtain it here. If you use the default parameters, the zram0 device will be 25% the size of available RAM. For example, if you have 1GB of available RAM, the created zram0 device will be 256MB in size, leaving you with 768MB of RAM. The default size is defined in the portion of the rc.local file shown below.

# Swap size = 25% of free memory;

echo $(($ZRAM_SWAP_SIZE * 1024 * 1024)) > /sys/block/zram0/disksize

In order to change the size to a fixed amount, comment the ZRAM_SWAP_SIZE= line and replace the string $(($ZRAM_SWAP_SIZE * 1024 * 1024)) with an integer representing the total number of bytes. As an example, to get a fixed size of 256MB of ZRAM, you would edit the rc.local file as shown below.

# Swap size = 25% of free memory;

# Swap size = 256MB
echo 268435456 > /sys/block/zram0/disksize

I understand the advantages of using a ZRAM device, and agree that storing virtual machines within ZRAM devices is a more efficient method for utilizing available RAM. What I struggle with is understanding how it is more useful on a desktop or laptop than a conventional swap partition, other than the speed advantage.