zram - Linux compressed ram block device: setup and script
Date: 12-02-2023 02:20 UTC
Linux has a facility to use random access memory as block device with compression. It uses lzo, lzo-rle, lz4 and zstd for compression. So saving space on ram. There is other ramdisk block device which does not uses compression.
Download the shell script zram.sh and use it to setup zram block device.
Alternatively you can use /sys directory files to setup it. You can view the script to know how to do it.
A brief explanation:
- Load kernel module with number of required devices (2): modprobe zram num_devices=2
- List zram devices: ls /sys/block | grep zram
- List compression algorithm: cat /sys/block/zram0/comp_algorithm
- Set compression algorithm: echo "lz4" > /sys/block/zram0/comp_algorithm
- Set maximum CPU compression streams(threads): echo 2 > /sys/block/zram0/max_comp_streams
- Set block disk size (e.g. 10 MegaBytes): echo 10M > /sys/block/zram0/disksize
- Set allocated ram memory limit (e.g. 5 MegaBytes): echo $((5*1024*1024)) > /sys/block/zram0/mem_limit
- Reset zram block device (To zero, empty): echo 1 > /sys/block/zram0/device/reset