Jump to content
Search

The new Ubuntu Wiki is live. Legacy content may be unavailable and page links may have changed. Read the announcement on Discourse.

Enable memory compression

From Ubuntu Wiki

You can enable random-access memory (RAM) compression on Ubuntu to improve how many applications can run at the same time.

On Ubuntu, the zswap memory compression feature provides the best results because Ubuntu already enables a swap file. The alternative zram feature is intended for systems with no swap storage.[1]

Choose the compression for your system

Depending on your hardware, different compression setups are recommended.

The following are some general tips. Feel free to experiment with different values and let us know about your experience.[2]

Low-memory and embedded systems

If your system has less than 2 GB of memory, keep memory compression disabled.

This is because even a very minimal installation of Ubuntu consumes almost 300 MB RAM. Reserving a section of memory for compression might not leave enough free memory for basic system operation.[3]

Between 2 and 8 GB RAM

On systems with relatively low memory, the main concern is to maximize the total available memory, even at the expense of latency performance. Strong memory compression can enable your system to run an application that otherwise wouldn't be able to work properly.

Use zswap with the zstd compression algorithm to maximize compression.

Slow disks

If your system has a very slow disk, such as a rotational disk (HDD), strong memory compression is useful to avoid disk access as much as possible.

Use zswap with the zstd compression algorithm to maximize compression.

Other desktop hardware

On desktop systems that don't fall under any of the previous categories, the main concern is latency performance. Systems with more than 8 GB RAM can run most applications. However, if you start too many of them at the same time, applications in the background might be swapped out to the disk. Switching back to such an application is slow. To speed up application switching, enable lightweight compression.

Use zswap with the lz4 compression algorithm, which decompresses data at RAM speed.[4]

Apply the settings

To ensure that the compression algorithms are loaded on your system, add them in the /etc/initramfs-tools/modules file:

# List of modules that you want to include in your initramfs.
# They will be loaded at boot time in the order below.
#
# [...]

lz4
zstd

Rebuild the initramfs image for the changes to take effect:

sudo update-initramfs -u

Open the /etc/default/grub file in a text editor. Add this configuration in your in the GRUB_CMDLINE_LINUX_DEFAULT string:

zswap.enabled=1 zswap.compressor=ALGORITHM

Replace ALGORITHM with either lz4 or zstd. Apply the configuration changes:

sudo update-grub2

Reboot your system.

Monitor the memory compression

Check the current state of zswap:

cat /sys/module/zswap/parameters/enabled

In the output, N stands for no (inactive) and Y stands for yes (active). Check the active compression algorithm:

cat /sys/module/zswap/parameters/compressor

Check the runtime compression statistics:

grep -r . /sys/kernel/debug/zswap

Notes