zram: A seriously cool way to do more with your ram
Got memory issues? Keep running out of ram? Want to store a ridiculously large file in ram because your disk (or network) is slow? If you're running linux, then look no further than zram - a hidden gem available since 3.14.
It's a little bit underdocumented, but thankfully easy to figure out, so I'm gathering everything I can find about it into one place.
zram is a kernel module for linux version 3.14 and above that allows you to use a portion of your ram to create virtual block devices (like
/dev/sdb3 for example) that actually compress data you put on them and store it in ram. It's installed by default (at least in ubuntu 16.10!) - just not enabled - so you should already have access to it if you're running a fairly recent kernel.
To start using it, you first have to enable it and load it into your running kernel. Despite how dangerous that might sound, it's actually quite safe (I think :P). Obviously, you'll need
sudo (or 'Administrator' for Windows fans) privileges to do this - and if you want it to persist across reboots, you'll have to edit some file like
/etc/rc.local or similar (though writing an init.d/upstart/system script/service file would probably be a better way to do it).
Here's the command to load the require kernel module:
sudo modprobe zram num_devices=2
In the above, I load the
zram kernel module, and tell it to create 2 virtual devices -
/dev/zram1 (why it's called modprobe when it actually loads the module I have no idea). You can have as many devices as you want here - they don't do anything until they are initialised.
Next, as you may have guessed, you need to initialise one of the new virtual devices in order to use it. That's fairly easy too:
sudo zramctl --find --streams 2 --size 2GB --algorithm lz4
I've specified a few more options here, so I'll go through them in turn.
--find- This tells it to use the next available and uninitialised device. It'll echo the path to the device it ended up using to your terminal once it's done initialising.
--streams 2- Sets the number of compression streams the device has. I'd recommend setting this to something around the number of cpu cores you have.
--size 2GB- Here I tell it the size of the virtual disk I want it to create. At most the device will use this amount of memory. I've specified 2GB here, but you should allocate a different amount based on your available ram -
MBare supported as suffixes here too.
--algorithm lz4- This is the compression algorithm I told it to use. Currently it only supports 'lzo' and 'lz4' - I'm not sure which one is better. If you do, let me know in the comments :-)
Now that the zram device is all initialised, we can start doing things with it! Need a place to put a few files? Try this command:
sudo mke2fs -t ext4 -O ^has_journal -L "zram device" /dev/zram0
In the above I format
/dev/zram0 to an ext4 partition. Note that I also disable journaling - journaling is system that ensures integrity in the event of an unplanned shutdown or loss of power - great for persistent drives like the one holding your operating system, but not so useful for one that'll get deleted automatically on shutdown anyway :-)
Perhaps you need a bit of extra speedy swap space because you don't have quite enough ram? That's easy too:
sudo mkswap --label 'zram swap parition' /dev/zram0 sudo swapon --priority 10 /dev/zram0 swapon
In the above I format
/dev/zram0 to be a swap partition, and then turn it on. The final command lists the current swap devices that are currently active. You can learn more about swap partitions here.
That concludes this post. Got any cool uses for this? Found something I've missed? Write a comment below!