• Bubble
  • Bubble
  • Line
zram swap partition in Linux - Compressed RAM
Andrei Clinciu Article AUthor
Andrei Clinciu
  • 2014-09-07T09:15:00Z
  • 2 min to read

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 zram. 

The zram module creates RAM based block devices named /dev/zram<id>(<id> = 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 #

sudo vim /etc/fstab

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

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

Andrei Clinciu
Andrei Clinciu

I'm a Full Stack Software Developer specializing in creating websites and applications which aid businesses to automate. Software which can help you simplify your life. Let's work together!

Building Great Software
The digital Revolution begins when you learn to automate with personalized software.

Find me on social media