ZRam is a kernel module that provides a compressed block device in RAM. This block device can be mounted as a swap device. When the system RAM starts to fill the kernel dynamicly compresses some of the information and moves it into the block device. This is much faster than a traditional swap as it all happens in ram. There is a small cpu hit. For systems with limited ram and storage or slow storage such as an old PC, Laptop or raspberry pi. With lz4 compression there is about a 2.5 compression ratio. However some space needs to be left in RAM for decompression of memory.
zRam documentation on kernel.org
zRam for impoving performance at wiki.archlinux.org
Create zRam on arch linux techrapiduk.blogspot.com
sudo modprobe zram
sudo bash -c "echo 'lz4' > /sys/block/zram0/comp_algorithm"
The size should not exceed 2.5 the size of the actual RAM. To find the systems RAM size in kb try this command.
grep MemTotal /proc/meminfo
For double that size in MegaBytes instead of KiloBites try this.
echo $(($( grep MemTotal /proc/meminfo | grep -o '[0-9]*' ) / 500))M
If that gave you a number that looks good. Use this command to set it.
sudo bash -c "echo $(($( grep MemTotal /proc/meminfo | grep -o '[0-9]*' ) / 500))M > /sys/block/zram0/disksize"
If the above commands didn't work for you. You can choose a number manually in M or G Megabytes or Gigabytes. With a command like this.
sudo bash -c "echo '2G' > /sys/block/zram0/disksize"
sudo mkswap --label zram0 /dev/zram0
sudo swapon --priority 100 /dev/zram0
If htop is installed it will show the active RAM/Swap size.
htop
sudo swapoff /dev/zram0
sudo rmmod zram
wget https://raw.githubusercontent.com/JeremiahCheatham/zRam/main/zram.service
or
curl -LO https://raw.githubusercontent.com/JeremiahCheatham/zRam/main/zram.service
sudo cp zram.service /etc/systemd/system
sudo systemctl enable zram
sudo systemctl start zram
sudo systemctl stop zram
sudo systemctl disable zram
sudo rm /etc/systemd/system/zram.service