ZRAM
Overview
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.
Kernel Configuration
To use ZRAM, the following kernel configuration is needed:
CONFIG_ZSMALLOC=m CONFIG_ZSMALLOC_STAT=y CONFIG_ZRAM=m CONFIG_ZRAM_WRITEBACK=y CONFIG_ZRAM_MEMORY_TRACKING=y
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.
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
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.
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.
Starting ZRAM at boot time
To make ZRAM devices automatically on system start-up, install the two scripts:
# apk add zram-config zram-config-openrc
Edit the configuration file /etc/conf.d/zram-init
. In accordance with the above considerations, the following minimal changes may be recommended:
num_devices=1 size0=<half of physical RAM, in MB> or 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