⬆️ ⬇️

Using zRam to increase the amount of available memory under Linux

image

I have been using the zRam module on my computers for 2 months now and I want to share the results. In practice, he allowed me without using the swap partition, and without receiving a visible slowdown of the computer, to increase the size of the RAM 2.5-3 times. On the virtual server, the same approach allowed a very noticeable increase in responsiveness due to lack of memory.

Interested please under the cat.



As wikipedia says



zRam is an experimental Linux kernel module (previously known as "compcache"). It improves performance by preventing paging to disk using a compressed block device in RAM, until you need to use the paging file on your hard disk. The speed of exchange with RAM is faster than with a hard disk, so zRam allows Linux to perform more swap operations, especially on older computers with a small amount of RAM.


For the user, everything looks like this: first you need to load the module (after having been compiled if it is missing)

')

modprobe zram num_devices=4 




Num_devices specifies the number of compressed block devices to be created.

For the most optimal use of the CPU, it is worth considering: the compression of each zram device is single-threaded. Therefore, I create them by the number of cores.



When configuring the module, the fixed size is NOT compressed data in bytes.



 SIZE=1536 echo $(($SIZE*1024*1024)) > /sys/block/zram0/disksize echo $(($SIZE*1024*1024)) > /sys/block/zram1/disksize echo $(($SIZE*1024*1024)) > /sys/block/zram2/disksize echo $(($SIZE*1024*1024)) > /sys/block/zram3/disksize 




This will create the device / dev / zram0 of the specified size.



 Disk /dev/zram0: 1610 MB, 1610612736 bytes, 393216 sectors Units = sectors of 1 * 4096 = 4096 bytes Sector size (logical/physical): 4096 bytes / 4096 bytes I/O size (minimum/optimal): 4096 bytes / 4096 bytes 




Data written to it will be transparently compressed in memory. What to do with it further is already your choice, I create swap partitions on these devices.



 mkswap /dev/zram0 mkswap /dev/zram1 mkswap /dev/zram2 mkswap /dev/zram3 swapon /dev/zram0 -p 10 swapon /dev/zram1 -p 10 swapon /dev/zram2 -p 10 swapon /dev/zram3 -p 10 




Further, the kernel itself already thinks what data to put there, depending on how often you access them and how much memory is free.



My experience shows that the compression ratio is usually 1 to 3.



In practice, this allowed for a laptop in 8Gb of memory to make a compilation of libreoffice in tmpfs. (it requires 7 GB of temporary files and approximately 1 GB of memory consumes each gcc stream when building).



The scope of this idea is extremely wide:





Addition:

Initially, development was conducted under the name compcache , and the first working versions were made for the 2.6.26 kernel (July 2008)

Starting from December 2009 and 2.6.33 kernel, it is available in the kernel, in the Staging section. For older kernels, patches are still available at the above site.

In kernel 3.8 should have been rendered from Staging, but this did not happen.

Source: https://habr.com/ru/post/172137/



All Articles