zram (previously called compcache) can create RAM based compressed block devices. It is a module of the Linux kernel since 3.2.
If physical swap devices exist, they will also be used by the system, but with a lower priority than a Zram device.
See Linux zram documentation and Zram for more info.
Similar results as with Zram can be achieved with Zswap, though Zswap needs a physical swap device (or swap file).
Automated setup
The packages zram-tools and systemd-zram-generator can be used to automatically setup zram devices.
zram-tools
zram-tools is available since Debian buster.
It currently only sets up zram devices on systemd-based systems.
To allow up to 60% of the RAM to be used as a zstd compressed swap space:
sudo apt install zram-tools echo -e "ALGO=zstd\nPERCENT=60" | sudo tee -a /etc/default/zramswap sudo service zramswap reload
sysvinit
zram-tools does not yet support sysvinit systems but there is an init script available. Download it and save it to /etc/init.d/zramswap, then make the script executable and then instruct your system to start it at boot time:
sudo chmod +x /etc/init.d/zramswap sudo apt install insserv sudo insserv zramswap
systemd-zram-generator
systemd-zram-generator integrates zram devices into systemd using generated swap (and ext2 mount) units. This allows managing them individually with systemctl when desired.
systemd-zram-generator comes with a default [zram0] section in /etc/systemd/zram-generator.conf. It defines a swap device which has 50% of available RAM or 4GiB, whichever is less.
Citing zram-generator's doc:
Run systemctl daemon-reload to create new device units.
Run systemctl start /dev/zram0 (adjust the name as appropriate to match the config).
Call zramctl or swapon to confirm that the device has been created and is in use.
Manual setup
zramctl from util-linux can list the zram devices present and their status and control them.
$ sudo zramctl NAME ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT /dev/zram0 lz4 4G 2.1G 248.6M 260.4M 8 [SWAP]