zram swap partition in Linux - Compressed RAM

// = zram swap partition in Linux - Compressed RAM :id: cb4c3c3e-38b6-46ed-bfe0-4c47560dab9b :author: Andrei Clinciu :website: https://andreiclinciu.net/ :publish_at: 2014-09-07 09:15:00Z :heading_image: \N :description: \N :type: article :tags: :keywords: :toc: left :imagesdir: ../assets/

image::{heading_image}[] So I bought a new SSD the other day. After reading about SSD issues with SWAP partitions i decided to investigate further.

I found out you can use something called https://www.kernel.org/doc/Documentation/blockdev/zram.txt[ *zram. *]

…. The zram module creates RAM based block devices named /dev/zram( = 0, 1, …). Pages written to these disks are compressed and storedin memory itself. These disks allow very fast I/O and compression providesgood amounts of memory savings. ….

Zram actually creates a loopback device which is stored in ram. It can be used for SWAP (vritual memory partitions); Storing cache files for web servers; storing static files; and many other things!

The cool thing is that it’s now built-in as a Linux module!

Let’s start b y using it as a swap replacement:

Remove swap from or place it into a #

[source,lang:default,decode:true]

sudo vim /etc/fstab

Put the following in /etc/init/zramswap.conf

[source,lang:default,decode:true]

description “Initializes zram swaping"start on runlevel [2345]stop on runlevel [!2345]pre-start script# load dependency modulesmodprobe zram num_devices=2# initialize the devices using 512 ram eachecho 536870912 > /sys/block/zram0/disksizeecho 536870912> /sys/block/zram1/disksize# Creating swap filesystemsmkswap /dev/zram0mkswap /dev/zram1# Switch the swaps onswapon -p 5 /dev/zram0swapon -p 5 /dev/zram1end scriptpost-stop script# Switching off swapswapoff /dev/zram0swapoff /dev/zram1rmmod zramend script

 

Then run + sudo start zramswap

Or as an alternative change swappiness + vim /etc/sysctl.conf + /vm.swappiness and change its value as desired. If vm.swappiness does not exist, add it to the end of the file like so:

vm.swappiness=10 + or install zram swap from ubuntu launchpad..

Now you won’t have a physical SWAP partition anymore, only a virtual one. This is best used with 8GB RAM or more. + It’s sometimes a little slower but hey, that’s the memory compression working:D

Subscribe to my Newsletter

Receive emails about Linux, Programming, Automation, Life tips & Tricks and information about projects I'm working on