LinuxCommandLibrary

zramctl

Manage ZRAM devices

TLDR

Check if zram is enabled

$ lsmod | grep [[-i|--ignore-case]] zram
copy

Enable zram with a dynamic number of devices (use zramctl to configure devices further)
$ sudo modprobe zram
copy

Enable zram with exactly 2 devices
$ sudo modprobe zram num_devices=[2]
copy

Find and initialize the next free zram device to a 2 GB virtual drive using LZ4 compression
$ sudo zramctl [[-f|--find]] [[-s|--size]] [2GB] [[-a|--algorithm]] [lz4]
copy

List currently initialized devices
$ sudo zramctl
copy

SYNOPSIS

zramctl [options] <device>

PARAMETERS

-f, --find
    Find the first available zram device.

-s, --size <size>
    Set the size of the zram device (e.g., 1G, 512M). If 0 is provided, the device will be removed.

-t, --streams <number>
    Set the number of compression streams.

-a, --algorithm <algorithm>
    Set the compression algorithm (e.g., lzo-rle, lz4, zstd).

-d, --reset
    Resets a zram device.

-l, --limit <size>
    Set the memory limit of the zram device.

-r, --remove
    Remove a specified zram device.

-v, --verbose
    Enable verbose output.

-h, --help
    Display help message.

-V, --version
    Display version information.

<device>
    Specify the zram device (e.g., /dev/zram0). Providing a device name triggers actions like setting size, algorithm etc.

DESCRIPTION

The zramctl command is a utility for managing zram (compressed RAM) devices in Linux. It allows you to create, configure, and destroy zram devices, which can significantly improve performance on systems with limited RAM by providing a compressed swap space in memory. It is typically used to set up zram devices as swap space, thereby leveraging RAM for compression and decompresssion instead of slow disk based swap.

It can set the compression algorithm used by the device (e.g., lzo-rle, lz4, zstd) and the size of the device. It provides a simple command-line interface for interacting with the zram kernel module, enabling users to easily create and manage zram devices according to their needs. It allows users to view information about existing zram devices, such as their compression ratio and amount of memory used. Its primary goal is to provide a user-friendly interface for managing zram devices, simplifying the configuration and monitoring process. It can be used in scripts and systemd unit files to automate the creation and management of zram devices at boot.

CAVEATS

Zram uses RAM, so allocating too much space can reduce available RAM. Ensure the selected compression algorithm is supported by your kernel. Correct sizes must be used to avoid errors. You need sufficient permissions to create or modify a zram device.

COMPRESSION ALGORITHMS

The choice of compression algorithm can significantly impact performance. lz4 is generally known for its speed, while zstd offers a good balance between speed and compression ratio. lzo-rle is also available and has performance characteristics depending on the content. Experimenting with different algorithms can help determine the best option for your specific workload.

DEVICE CREATION EXAMPLE

To create a 1GB zram device using lz4, you can use the command: zramctl --find --size 1G --algorithm lz4. After creation, you can then format it as swap space: mkswap /dev/zram0, followed by enabling the swap space with swapon /dev/zram0.

TROUBLESHOOTING

If you encounter issues with zram devices, check the system logs (e.g., using journalctl -k) for any error messages related to zram. Ensure the zram kernel module is loaded (lsmod | grep zram). Double-check the size and algorithm parameters when creating the device.

HISTORY

Zram was originally developed as part of the compcache project to reduce I/O and improve system responsiveness, especially on devices with limited RAM. The zramctl command provides a command-line interface for managing zram devices, which were previously managed through direct sysfs manipulation or custom scripts. It's been available in many Linux distributions for several years, becoming a standard tool for managing zram.

SEE ALSO

mkswap(8), swapon(8), swapoff(8)

Copied to clipboard