Modern devices have GBs of RAM and swapping is not very common without running lots of applications together. But what if you have a older device with 512MB or less RAM? You’ll see a lot of data being swapped in and out of your hard disk swap partition. Hard disks are much slower than RAM and thus swapping enables you to use virtual memory at the cost of performance.
zram is a Linux kernel feature (enabled from version 3.2) to use a compressed virtual memory in-RAM. It mimics a LZ4 compressed block device in the RAM for swapping data. As compression is used, more data can be stored in RAM than in actual capacity. The technique uses more processing to compress and decompress data but the lag is negligible when compared to swapping data in and out of hard disk.
To enable zram on Ubuntu, run:
$ sudo apt-get install zram-config
The feature is initialized as soon as you install it. By default it uses half your RAM as compressed swap. To view the device nodes and amount of RAM used by zram, run:
$ cat /proc/swaps Filename Type Size Used Priority /dev/zram0 partition 492980 0 5 /dev/zram1 partition 492980 0 5 /dev/zram2 partition 492980 0 5 /dev/zram3 partition 492980 0 5
When used, the Used field changes.
To tweak the amount of RAM used by zram, open /etc/init/zram-config.conf and find the following line:
mem=$(((totalmem / 2 / ${NRDEVICES}) * 1024))
As you can see, mem is calculated as half the (total RAM available / number of CPU cores) in KBs. The zram devices are created one per CPU core and the memory is distributed equally (refer the output of /proc/swaps above).
Why use this rather than Zswap?
zswap is a RAM based compressed cache for regular swap devices. So it can be used only when you have a swap configured. zram is itself a swap device using half your RAM by default.
So it would make more sense to use ZRAM on a laptop that has only an SSD and Zswap on a desktop that has an SSD as well as a HDD for a swap?
Right on spot.
When I open zram-config.conf the “mem=$(((totalmem / 2 / ${NRDEVICES}) * 1024))” line is missing, this is all I get:
description “Initializes zram swaping”
author “Adam Conrad ”
start on runlevel [2345]
pre-start exec /usr/bin/init-zram-swapping
pre-stop exec /usr/bin/end-zram-swapping
It’s probably specific to your system. It worked fine for me. You can try adding the line(s) manually. If it still doesn’t work, please contact the project author/maintainers.