I like to run a lot of VM's, whenever I start work on a non-trivial project I usually make a VM to isolate the system a bit. This means I run a lot of VMs from time to time.

One of the problems with virtualization is the increase in memory usage, one of the ways I've tried to counter this is by using KSM (Kernel SamePage Merging) which merges pages in memory containing the same data. My server has 8 GB of memory, this saves me about 1 GB in average.

A friend of mine keeps talking about how awesome zram is so I gave it a shot. What it does is allocate memory to a compressed block device, this block device can then be used for swap. Swapping from/to compressed memory is super-fast compared to traditional swapping. One of the problems with zram is an obvious processing-overhead caused by the constant compression/decompression of pages to/from the swap.

Here's a short explanation of the things I did to enable zram.

Upgrading Debian

I like to run stable software (especially for the system my hypervisor(KVM) lives on), and it's hard to get more stable than a Debian stable (CentOS anyone?), this means running a 2.6.32 kernel (in the case of squeeze/6.0). Since wheezy (or 7.0) is currently in RC1 I decided to upgrade. Needless to say this went smooth. I now run a 3.2 kernel.

Modprobing the module

For debian all I need to do to modprobe at boot is to add "zram" to /etc/modules. One of the things you'd want to give the module as parameter when probing is zram_num_devices, this tells zram how many devices you want. Usually you want as many zram-devices as the number of CPUs on the system.

On debian this is done by making a file in /etc/modprobe.d and entering something like (in my case 2 cores):

options zram zram_num_devices=2

Initializing the SWAP on boot

Since the content of DRAM (Dynamic RAM) is lost when the module are unpowered you need to make new swap every time you boot. There are lots of nice init-scripts out there to do this for you. I like to do it myself, my solution (albeit primitive) is to put the details in /etc/rc.local. First I tell the zram-devices how many bytes I want in each, then I make the swap and then i mount the swap. Details below:

# zram swap
# expects zram modprobed with zram_num_devices=2
# allocates 4gb mem to zram, in 2 separate swap partitions.
echo 2147483648 > /sys/block/zram0/disksize
echo 2147483648 > /sys/block/zram1/disksize
mkswap /dev/zram0
mkswap /dev/zram1
swapon -p 100 /dev/zram0
swapon -p 100 /dev/zram1

The reason for using 4gb of my memory as zram is because my machine has a total of 8 gb of memory. Some scripts use 100% of the memory as zram.

Some statistics

I've located a script on the old zram site, that prints some statistics about the zram's at runtime, it can be found here:

http://compcache.googlecode.com/git/sub-projects/scripts/zram_stats

As always YMMW and I'll be tweaking my setup over the course of time as I run into new bottlenecks.