Set Up Temporary Swap with zramctl
zramctl turns spare RAM into a compressed swap device, and it will bite you if you treat the size like a throwaway detail. This walkthrough creates a zram block device, initialises it as swap, checks the kernel is actually using it, then tears it down cleanly. Allow about ten minutes.
The route
Jump straight to the step you need, or tick off Done means at the end.
- Linux host with zram support. The kernel module needs to be available before
zramctlcan do anything. - The
zramctlcommand from util-linux. Check which binary you are actually running: on the reference machine, the Debian package reports util-linux 2.39.3, but the shell'szramctlresolves to Homebrew util-linux 2.42.4. Mismatches like that explain half the "it worked on my other box" reports. - A real size, not a copied one. zram stores compressed pages in RAM, so it is not extra physical memory: pick a size for the workload you intend to run, don't just paste
1Gbecause a guide used it.
The workflow below uses options documented by the local zramctl(8) manpage. It changes live swap state and needs elevated privileges, so nothing here is undo-free once you hit enter.
1. Check the command and the host
Run the read-only checks first, as your ordinary user:
$ command -v zramctl
$ zramctl --version
$ zramctl --help
$ zramctl
$ ls -l /dev/zram* 2>/dev/null || true
No output from the final two checks is a valid starting point. zram device nodes are not necessarily created at boot, and zramctl without options only reports devices with a non-zero size, so do not assume /dev/zram0 exists just because zram is supported.
Checkpoint
Record the version and the path printed by command -v. If the command is missing, install or enable the host's normal util-linux and kernel support before continuing; do not download a replacement binary into a system path just for this test.
2. Choose a free device and size
Use --find rather than guessing a device number: with a size attached, it finds the first unused zram device and initialises it in one operation. That changes kernel device state, so run it with sudo and keep the output:
$ sudo zramctl --find --size 1G
/dev/zram0
- Your device may have another number. Never hardcode
/dev/zram0from a previous run. - The size suffix is binary.
Gmeans the same asGiB, andMmeansMiB. A requested size that is not a multiple of the memory page size gets rounded up. - Disk size is not RAM used. The device's disk size is the limit for uncompressed data; the RAM consumed by its compressed contents is a different number entirely.
Warning
Do not run this against a device already used for swap, a mounted filesystem or data you need. --find --size selects an unused zram device, but a device already created can still get picked incorrectly if another tool is changing devices at the same time. On a busy host, coordinate with whoever owns zram there.
3. Verify the new device before enabling it
Set a shell variable to the exact path from the previous step. This is an ordinary shell assignment, not a privileged operation:
$ ZRAMDEV=/dev/zram0
$ zramctl
NAME ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram0 zstd 1G 0B 0B 0B 4
$ zramctl --noheadings --output NAME,DISKSIZE,ALGORITHM,STREAMS "$ZRAMDEV"
/dev/zram0 1G zstd 4
Formatting varies with util-linux version and the host's kernel, so don't panic if your columns look different. What matters: the device name, a non-zero disk size and a selected compression algorithm. The algorithm list in the manpage is only a guide, since kernel configuration can make it incomplete. If the selected device is not the one you created, stop and investigate before formatting it.
4. Initialise and enable swap
Write a swap signature and activate the device. Both commands need elevated privileges:
$ sudo mkswap "$ZRAMDEV"
Setting up swapspace version 1, size = 1 GiB (1073737728 bytes)
no label, UUID=EXAMPLE-UUID
$ sudo swapon "$ZRAMDEV"
$ swapon --show "$ZRAMDEV"
NAME TYPE SIZE USED PRIO
/dev/zram0 partition 1G 0B 100
The UUID is host-specific and the displayed size may be rounded. A successful swapon --show confirms the kernel considers this device active. If swapon fails, read its error before retrying: common causes are an existing signature or swap entry, a device that was never initialised, or a host policy rejecting the configuration.
This example does not touch /etc/fstab or a boot service. The swap disappears after swapoff and will not return after reboot unless something else enables it.
5. Monitor usage without changing state
These are read-only while the temporary swap is available:
$ swapon --show
$ zramctl --output NAME,DISKSIZE,DATA,COMPR,TOTAL,COMP-RATIO,MOUNTPOINT
NAME DISKSIZE DATA COMPR TOTAL COMP-RATIO MOUNTPOINT
/dev/zram0 1G 0B 0B 0B 0.00
DATA is the uncompressed amount stored in zram, COMPR is the compressed amount, and TOTAL includes allocator and metadata overhead. Values sit near zero until something actually uses the swap. A large disk size is not a promise of that much free RAM.
6. Remove the device safely
When the maintenance task is finished, stop using the swap before resetting the device. This is the recovery path as well as the normal cleanup path:
$ sudo swapoff "$ZRAMDEV"
$ swapon --show "$ZRAMDEV"
$ sudo zramctl --reset "$ZRAMDEV"
$ zramctl
$ ls -l "$ZRAMDEV" 2>/dev/null || true
swapoffmay take time. Pages are moving out of swap and it needs free RAM to do it. Do not interrupt it casually on a busy host.- Reset is destructive. It frees zram's allocated memory and returns its disk size to zero, so never reset a device still mounted or used by another service.
- If
swapoffreports the device busy, find the owning process or service and resolve that dependency first; do not force a reset. - The device node may survive the reset. After reset,
zramctlnormally prints no row for that device because its size is zero, but the node itself often stays. That's fine: an unused node can be picked again later with--find.
Common traps
- Using a guessed device.
/dev/zram0is an example, not a guarantee: keep the path--findactually returned. - Changing settings after initialisation. zram settings only change after a reset, so finish the configuration before setting the size, or reset and recreate the device.
- Confusing compression with capacity. Disk size describes uncompressed data; actual RAM use depends on the data, compression algorithm and allocator overhead.
- Skipping
swapoff. Resetting active swap can disrupt the host and destroy data that has not been moved elsewhere. - Trusting the algorithm list completely. The local kernel's crypto configuration decides what is usable. Check
/sys/block/zram0/comp_algorithmonly for a device that exists, and treat its list as a guide, not a guarantee.
Done means
- You checked the actual binary and version of
zramctlbefore trusting its behaviour. --find --sizereturned a device and you recorded its path.- The device showed a non-zero disk size before activation.
swapon --showconfirmed active swap.- You ran
swapoffbeforezramctl --reset, not the other way round. - The temporary device now has zero size and no active swap entry.