Compressed Memory

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.

About swap

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.

About zRam

zRam is a kernel module that was introduced in the linux kernel 3.2. Previously it was also called zSwap or compcache. 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.

Disadvantages

As 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.

Advantages

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.

Using zRam

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:

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 internals

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 /sys/block/zram<id> directory.

  • disksize
  • orig_data_size
  • compr_data_size
  • comp_algorithm

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>/compr_data_size and /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.

Using different compression algorithms

Sources

2 thoughts on “Compressed Memory

  1. “zRam is a compressed ram disc”
    No it’s not. It’s a block device that is based on RAM. As such it is somewhat like a hard drive, it can hold data but right after creation there is no structure. Just as with a HDD you need to format the ZRAM block device (e.g. mkfs.ext2) and mount it. Then you can store data there.
    If you want to use it as SWAP device you need to execute mkswap and swapon.

    IMO it would be nice to mention that other compression algorithms are available – e.g. lz4 – actually all those provided by the crypto API. The kernel .config file would contain CONFIG_CRYPTO_LZ4=m or CONFIG_CRYPTO_LZ4=y if lz4 can be set as compression algorithm. AFAIK lz4 compresses a little worse than lzo but is noticeably faster.
    More compression algorithms will be available when they are set in the section “# Compression” of the kernel .config file (then make …) then doing a reboot with using the new kernel.

    • You’re absolutely right. I think the description was a little bit unprecise. I corrected that in the article. I also added a section that describes the stuff concerning the other compression algorithms. When having a look at the benchmark (the last link in the sources) that tests various algorithms you’re right that lz4 seems to be faster than lzo. Maybe the major advantage is the little memory consumption of lzo. But of course you can replace lzo by lz4 for customizing the zram swap for your needs. I was only intended to describe how such a compression scenario works.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.