Friday, October 7, 2011

Enable Zram in Fedora 15

While trying to optimize the elementary OS performance, Sergey Davidoff stumbled upon a project called compcache that creates a RAM based block device which acts as a swap disk, but is compressed and stored in memory instead of swap disk (which is slow), allowing very fast I/O and increasing the amount of memory available before the system starts swapping to disk. compcache was later re-written under the name zRam and is now integrated into the Linux kernel.

I decided to give it a try, and the result on my desktop with a quad-core CPU and 2Gb of RAM was fantastic: instead of freezing after running out of RAM, the system worked like nothing happened. I didn't notice any difference at all. It looked just like adding more RAM! Surprisingly, I got almost the same results on a 6-year-old laptop with Pentium M and 1Gb of RAM! So, I've improved the script to automatically adapt to the amount of memory in the system and automatically scale across several CPUs or CPU cores, packaged it in .deb and uploaded to PPA.

- Sergey Davidoff

Sergey also mentions that the only thing that prevents this from being enabled by default in elementary OS Luna for now is the existence of Atom netbooks with fast SSDs for which he doesn't know if this would be useful or not.

This is especially useful for netbooks, old computers (or computers that don't have too much RAM), virtualization or embedded devices but of course, you can use it on any computer.

What follows are instructions on how to enable zram for Fedora. You need Linux kernel version2.6.37.1 or better with zram compiled as a module.

Run the following command to see if you have zram available as a module in your kernel.
modprobe zram 

If the above command runs without errors, you have zram available as a module in your kernel. If not, you need to rebuild your kernel to have zram compiled as a module. Refer to these instructions, here, to learn how to compile a custom kernel for fedora.

There are 3 files you need to enable zram for Fedora. They are:

  • zramstart (Bash Script)
  • zramstop (Bash Script)
  • zram.service (Systemd Service File)
Get these files from here.

Place zramstart and zramstop in the following folder.

Place zram.service in the following folder.

As root, run the following commands to enable zram for Fedora.
systemctl daemon-reload
systemctl enable zram.service
systemctl start zram.service

Run the following command to make sure the zram service started properly.
systemctl status zram.service

The output should look like this.
zram.service - Enable compressed swap in memory using zram
Loaded: loaded (/etc/systemd/system/zram.service; enabled)
Active: active (exited) since Sun, 02 Oct 2011 22:46:31 -0400; 44min ago
Process: 942 ExecStart=/usr/bin/zramstart (code=exited, status=0/SUCCESS)
CGroup: name=systemd:/system/zram.service