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).

Here's an excellent article that explains how ZRAM and ZSWAP work and what the differences are. It can be very helpful in determining which one is best to use and how.

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

Then edit /etc/default/zramswap, change lines below:

Note: Depending on your zram-tools version, you may need to uncomment the relevant lines before editing.

ALGO=zstd
PERCENT=60

Restart the service:

sudo systemctl restart zramswap

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:

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]