Home » Article » ZRAM on Debian/Ubuntu for Memory Overcommitment

ZRAM on Debian/Ubuntu for Memory Overcommitment

Pages

October 2013
M T W T F S S
« Sep   Jan »
 123456
78910111213
14151617181920
21222324252627
28293031  

RSS Link

  • An error has occurred; the feed is probably down. Try again later.

Twitter Updates

Blog Stats

  • 346,176 hits

Archives

Pengunjung


In recent Linux releases, it’s available a tiny module called zram, that permits us to create RAM based block devices (named /dev/zramX), which will be kept in memory as compressed data. These ram-based block devices allow very fast I/O, and compression provides a reasonable amounts of memory saving.

We can use it as a drop-in replacement for the well-known tmpfs (used for speeding up compilation tasks or for /tmp), or better as a primary swap device, that will lead to virtually increase memory capacity, at the expense of a slightly increased CPU usage to compress/decompress the swapped data.

Nowadays RAM is very cheap, so why bother with compression? Because there are some situations where you can’t upgrade memory (netbooks) or you want to over-commit real resources (virtualization hosts).

For Ubuntu Precise and later:

Starting with Ubuntu Precise, there is an official upstart script for Ubuntu by Adam Conrad to configure zram in the main repository:

1
sudo apt-get install zram-config

For other distributions or older Ubuntu:

Googlin’ around to find a nice way to configure zram devices as swap, I found a very nice upstart script that will create a bunch of ramz devices depending on the number of CPU cores available, with a total size of the available memory: https://raw.github.com/gionn/etc/master/init.d/zram

Copy the script to the init.d folder, mark it as executable and enable autostart on boot:

1
2
3
sudo wget https://raw.github.com/gionn/etc/master/init.d/zram -O /etc/init.d/zram
sudo chmod +x /etc/init.d/zram
sudo update-rc.d zram defaults

Try it manually executing it for the first time with:

1
/etc/init.d/zram start

Depending on the kernel version you are running, you may need to adjust the module parameter name to num_devices on line 26 to:

1
modprobe zram num_devices=$num_cpus

or keep as is for newer kernels:

1
modprobe zram zram_num_devices=$num_cpus

Checking if it’s working

If everything went smooth, you will find a few notices on dmesg:

1
2
3
4
5
6
zram: module is from the staging directory, the quality is unknown, you have been warned.
zram: Creating 4 devices ...
Adding 1497864k swap on /dev/zram0.  Priority:100 extents:1 across:1497864k SS
Adding 1497864k swap on /dev/zram1.  Priority:100 extents:1 across:1497864k SS
Adding 1497864k swap on /dev/zram2.  Priority:100 extents:1 across:1497864k SS
Adding 1497864k swap on /dev/zram3.  Priority:100 extents:1 across:1497864k SS

meaning that the zram device have been created and enabled as swap devices with highest priority.

You can discover the increased swap space available with free -m:

1
2
3
4
         total       used       free     shared    buffers     cached
Mem:          5851       5696        154          0         85       4310
-/+ buffers/cache:       1300       4550
Swap:         5851          0       5850

Happy zramming!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: