zram swap partition in Linux - Compressed RAM

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 stored
in memory itself. These disks allow very fast I/O and compression provides
good 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 modules
modprobe zram num_devices=2

# initialize the devices using 512 ram each
echo 536870912 > /sys/block/zram0/disksize
echo 536870912> /sys/block/zram1/disksize

# Creating swap filesystems
mkswap /dev/zram0
mkswap /dev/zram1

# Switch the swaps on
swapon -p 5 /dev/zram0
swapon -p 5 /dev/zram1
end script

post-stop script

# Switching off swap
swapoff /dev/zram0
swapoff /dev/zram1

rmmod zram
end 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

You might enjoy these similar articles:

Be the first to comment!

Add a new comment

All comments are moderated and must adhere to the terms of service.

Subscribe to my awesome newsletter!








What to expect: Ultimate Knowledge regarding Business Efficiency, Personalized Marketing Experience, Software Development and Cyber Security tips and tricks. 1-3 mails per month. Unsubscribe any time. See the privacy policy to learn how we take care of your information.