From postmarketOS
Jump to navigation Jump to search


Zram uses a part of physical RAM to emulate a block device and use it for swapping. In essence, in compress unused RAM content within itself, to leave more memory for the currently active one. It can also be used to host the /tmp filesystem but this is not practical.


The Linux kernel on your device must be configured for ZRAM use during compile time. A simple check is

zcat /proc/config.gz | grep CONFIG_ZRAM=m

If the output is empty, you can try to build your own kernel, see "Kernel configuration" below.

Manual ZRAM setup

ZRAM is built as a module. First, load it, specifying the number of ZRAM devices to create:

# modprobe zram "num_devices=NUMBER"

Configure these ZRAM devices with zramctl. Specify, at least, the size of memory that you'd like to dedicate to a particular, or to the first available, device:

# zramctl -f -s SIZE

See zramctl --help for more keys.

Make it (e.g. zram0) a swap device, activate it and check:

# mkswap /dev/zram0
# swapon /dev/zram0
# swapon --show

ZRAM settings strategy

The simplest solution to begin with is probably a single ZRAM device of 1/2 of physical RAM. Monitor RAM usage and do further investigation yourselves.

Optimal ZRAM settings may vary greatly depending on particular hardware configuration, set of installed applications, particular use cases and user's habits. Thus it is not easy to recommend a universal setup that would equally fit everyone. For example:

  • the zram-init script from pmOS repository suggests making two ZRAM devices: 512MB for swap and 2GB for /tmp. However, the former seems too little for modern hardware with 3 to 8GB RAM; and the latter is definitely too little and is rejected by system, thus it remains unused.
  • the zram-config script used in Debian-based distributions makes a dedicated ZRAM device per CPU core, totaling 1/2 of physical RAM. This does not seem the best option for pmOS devices, as compared to desktops: with less memory, greater cores number, and unequal cores.

Automatic ZRAM setup (at boot time)

To make ZRAM devices automatically on system start-up, install the two scripts:

# apk add zram-init zram-init-openrc

Edit the configuration file /etc/conf.d/zram-init. In accordance with the above considerations, the following minimal changes may be recommended:

size0=<half of physical RAM, in MB>
size0=`LC_ALL=C free -m | awk '/^Mem:/{print int($2/2)}'`

To use lz4 (the fastest and easiest) or lzo compression, algo0 should also be set explicitly. The default is zstd (the most efficient but also the most CPU-hungry one). Settings for devices beyond the specified number are ignored anyways.

Add the script to boot runlevel:

# rc-update add zram-init boot 

Kernel configuration

To use ZRAM, the following kernel configuration is needed:


or, in terms of menuconfig tree,

Memory Management options  --->
    <M> Memory allocator for compressed pages
    [*]   Export zsmalloc statistics
Device Drivers  --->
    [*] Block devices --->
        <M> Compressed RAM block device support
        [*]     Write back incompressible or idle page to backing device
        [*]     Track zRam block status

Additionally, to use the LZ4 compression algorithm (the least efficient and the fastest), it must also be enabled:

Cryptographic API --->
    *** Compression ***
    <*> LZ4 compression algorithm

Remove the old setup, configure the kernel as said above, rebuild it and redo the setup:

$ pmbootstrap -y zap -p
$ pmbootstrap kconfig edit YOUR-KERNEL-PACKAGE
$ pmbootstrap build --force YOUR-KERNEL-PACKAGE
$ pmbootstrap install ...

See Kernel configuration for package naming and other details.