Quick Usage[edit | edit source]

Load the zram kernel module. This should create /dev/zram0 which you can configure with zramctl.

## Load the zram module. It should have created /dev/zram0 by default.
# modprobe zram
# ls -al /dev/zram*
/dev/zram0

## Define the disk size and algorithm of zram0
# zramctl --size 128m --algorithm lz4 /dev/zram0

## Make a filesystem and mount it
# mkfs.ext4 /dev/zram0
# mount /dev/zram0 /mnt
# df -h /mnt
Filesystem      Size  Used Avail Use% Mounted on
/dev/zram0      108M   84K   99M   1% /mnt

The size of the zram0 device is its blocksize which df reports is around 128MB (minus ext4 metadata and other overheads). The actual memory consumption of the block device can be viewed with zramctl. After running dd if=/dev/zero of=test.bin bs=512 count=100000, we now see the filesystem being approximately 50% full whilst only using 4.1KB of actual memory.

## Write all zeros to a file, which should compress very well
# dd if=/dev/zero of=/mnt/test.bin bs=512 count=100000
100000+0 records in
100000+0 records out

## The filesystem is now 50% used
# df -h /mnt
Filesystem      Size  Used Avail Use% Mounted on
/dev/zram0      108M   49M   51M  50% /mnt

## But it's only using 4.1KB of memory
# zramctl
NAME       ALGORITHM DISKSIZE  DATA COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram0 lzo           128M 53.9M  4.8K   60K       4 /mnt

You may define an upper-limit to the amount of memory that the zram device can use by writing to /sys/block/zram0/mem_limit. For example, to limit the total memory to 10MB:

## Maximum of 10MB of memory (after compression)
# echo 10m >  /sys/block/zram0/mem_limit

Use this with care since exceeding this value will cause writes to fail and will most likely corrupt the filesystem using this device. Continuing on with the example above, even though /mnt appears to be 128MB in size, because zram0 can only use 10MB of ram, we cannot write data that compresses larger than 10MB. So, if we were to run:

## Writing 50MB of random data
# dd if=/dev/urandom of=test.bin bs=512 count=100000
# df -h /mnt
Filesystem      Size  Used Avail Use% Mounted on
/dev/zram0      108M   49M   51M  50% /mnt

## Actual memory usage is capped to 10MB, so something got truncated...
# zramctl
NAME       ALGORITHM DISKSIZE  DATA COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram0 lzo           128M 10.1M   10M   10M       4 /mnt

## Uh-oh
# dmesg | tail
[ 2793.272512] EXT4-fs warning (device zram0): ext4_end_bio:323: I/O error 10 writing to inode 12 (offset 8388608 size 2101248 starting block 7194)
[ 2793.274915] buffer_io_error: 12490 callbacks suppressed
[ 2793.274917] Buffer I/O error on device zram0, logical block 7194
[ 2793.277186] Buffer I/O error on device zram0, logical block 7195
[ 2793.278343] Buffer I/O error on device zram0, logical block 7196
...


Tasks[edit | edit source]

Available compression algorithms[edit | edit source]

The algorithm that is available can be listed from the comp_algorithm pseudofile.

# cat /sys/block/zram0/comp_algorithm
lzo [lz4]

Mapping swap to zram[edit | edit source]

You can try to 'increase' your system's memory by putting your swap file in zram.


See Also[edit | edit source]