ZRAM as Swap Device on Embedded Linux Systems

  • Memory Swapping - Reminder

Linux OS manages physical RAM (Random Access Memory) in chunks called pages. Paging is and essential component of Virtual Memory mechanism.

Swapping is a process intended to free up RAM by fetching of less frequently used pages from RAM and coping them into preconfigured place on external storage, such as Hard Disk.

For example, many applications requires more pages on startup than amount of pages necessary for their steady state running. Kernel will swap out less frequently used pages and free the RAM when it's become necessary.

Swapping consumes CPU cycles and also depends on speed of write/read to/from external storage. On heavy loaded systems suffering from low free RAM, more swaps out/in occurs meaning degradation in system performance.

The total amount of memory available to applications in a Linux OS is the (RAM space + Swap space).

Linux supports swapping to Swap Partition/Disk and Swap File.

  • ZRAM

ZRAM is a Linux Kernel module which creates Compressed RAM Block Device. Data written to this RAM-based storage will be compressed and decompressed on reads.

ZRAM Disk can be used for /tmp or /var folders and even for Swap.

  • ZRAM Disk as a Swap Disk on Embedded Systems

From my point of view, page swapping from RAM to RAM including CPU overhead for copy and compression/decompression on embedded systems - sounds like a really crazy idea.

But, under some specific circumstances crazy idea may become a only possible solution. For example, if you are running out of free RAM and fighting for additional free space, you probably need to consider this solution to be used on your system.

  • What should be enabled in Linux Kernel build configuration?

CONFIG_SWAP

CONFIG_ZRAM

CONFIG_ZRAM_LZ4_COMPRESS

CONFIG_ZSMALLOC

  • What should be configured in Linux Kernel at runtime?

tip: swapon, swapoff - enable/disable devices and files for paging and swapping. swapon utility have -p switch which sets the priority for attached swap facility, the higher values indicates higher priority:

-1 (default) <= valid value <= 32767

  • Analyzing the System

Use one of the following facilities to get system statistics regarding the Swap and ZRAM Disk:

ZRAM Disk Statistics exposed through the Sysfs

/sys/devices/virtual/block/zramX

  • Performance tuning

Of course, performance tuning is a very system dependent and multiple factors should be taken in account.

Let's review configuration option which is most influence on behavior of swapping in the system:

vm.swappiness

Is a property for the Linux Kernel which changes the balance between swapping out runtime memory (process's memory) as opposed to dropping pages or write back from system page cache (files and folders cache).

0 <= swappiness (60 default) <= 100, a low value means Kernel will avoid swapping but prefer dropping where highest value means more swappy system and same preference/priority for swapping and dropping.

tip: What Is Swappiness on Linux?