When using swap the system is going to be dramatic slow and maybe hangs for seconds as the performance of the next level memory is dramatically slower then random access memory. In case there is not enough physical RAM and swapping happens really often, there is a way to compensate this approach a little bit by using compressed in-memory swap space.
In a unix based system there is some memory called swap. Swap is used for mainly two purposes. First as extended memory in case the physical memory is not big enough. Without swap there is a risk of data loss. So swap is important to prevent such situations. But with the implication that the system gets really slow as the access times of a hard disc is another dimension compared to RAM. And secondly for hibernation. In this case the content of the transient memory is stored to the persistent hard disc, ssd or whatever. For that purpose the size of the swap space must be at least the size of the RAM. In this case there sequential rw is important.
zRam is a kernel module that was introduced in the linux kernel 3.2. Previously it was also called
zRam is a compressed block device that uses the high speed compression algorithm
lzo. Alternatively there is
lz4 available. It is times faster than
gz that is widely used. It’s compression ratio is not as good as the compression ratio of
gz. For having a comparison of different compression algorithms, please have a look this wiki or this blog. The speed of the compression is extraordinarly important as the amount of data that is compressed with
zRam is dramatically higher than when compressing files or an archive.
zRam de/compresses the memory, some cpu time is necessary to perform theses operations. So it significantly slows down the system. On a mobile device more cpu time also decreases the time a battery powers the system.
Comparable to a normal swap space that is used to extend the system main memory,
zRam is like a rocket to a horse. So when there is no possibility to get more main memory, this approach is much faster than using classic swap on a hard disc or solid state disc for this purpose.
For using zRam for compressed memory it’s possible to create a block storage that then will be used as swap space. This block storage will be stored within the memory too.
When using zRam the simplified steps are:
modprobe zram # adds the zRam module to the kernel
echo $SIZE > /sys/block/zram0/disksize # defines the size of the device
mkswap /dev/zram0 # creates a swap device out of the zram block device
swapon /dev/zram0 -p 10 # enables the device as swap
# the priority is higher than default, thus it is preferred compared to default swap
But there is a script that handles all that stuff by installing a service and setting up a
zRam device. Furthermore this script is setting up a device for each physical processor. Thus a multicore processor can be used most effectivly as the full multithreaded power is used. Furthermore unloading is supported too, as well as priority mapping of the swap devices to force the kernel to use the
zRam device before using the swap on the hard disc.
Some useful things when using zRam
zRam exposes its internals via file system. The directory
zRam stores all the information is
/sys/block/zram<id>. For a detailed information please have a look at the documentation at kernel.org. The following list describes numerous interesting values that can be accessed via the file structure. They are stored within the
With these values it is possible to have a look to how it works and how it is used. For further analysis how good it works and whether it improves the system performances these values can be used.
Looking up the compression ratio
When having a look at
/sys/block/zram<id>/orig_data_size it is possible to calculate the compression ratio. By using this data the effective “win” in ram can be calculated. Based on different sources, the compression ratio should be at least 1:2. For these sources please have a look to the sources section. This means that for 1 GiB of memory the compressed size is at maximum 512 MiB. So there is additional 512 MiB memory that may be used for other purposes.