/etc insider

since 1999 (and still editing)

ZRAM and bcache as swap storage

| Comments

Use of ZRAM for swap is not new approach. In case you missed it, ZRAM kernel module makes “virtual” block device that is in fact RAM backed storage – like RAM drive, but with one important difference: the data in that storage is compressed, so, depending of the nature of those data, memory consumption would be less than of usual RAM drive. Say, we have 6 GB RAM available and then we allow to allocate up to 1 GB of physical memory for ZRAM drive. In general we can expect factor 2 compression (and even more for text data), so, it means we loose 1 GB, but get 2 GB in return.

Our system would have now only 5 GB RAM, but plus 2 GB of swap disk on very fast storage. I used this setup quite soon after ZRAM was brought into vanilla kernel.

Since RAM disks are very fast, this might be true improvement – unless you get constant swapping, that would be noticeable anyways, in despite of having lightning fast RAM storage. But there’re no wonders – you can’t just have all for nothing, even if you engineered it. If you still have high memory pressure in your system, it’s time to go get more RAM in shop, indeed.

One thing did always bothered me with it though – I’d prefer ZRAM to have real (hard or SSD) disk storage attached so it could place inactive (less recently used) pages there. RAM is precious resource and having it filled with anything that system wouldn’t need soon is a real waste, even if it’s compressed.

Should I say that as soon as bcache (and other similar projects) evolved, I tried making that combination. Alas, a few of them just caused kernel panic, and as to bcache – it refused to make a pair with ZRAM drive at all. Some time after I found a solution, although, and here I’m going to present it to you.

So, let’s begin – first of all you’d need to get bcache. The module itself goes with vanilla kernel since version XXX, but we’d need bcache-tools as well in order to interact with the module. I’d assume that you have it downloaded into /usr/local/src/bcache-tools and compiled it there.