A successor to compcache, zram, has been already integrated in the Linux kernel for a while now. This means that no additional compilation nor tweaking is required to benefit from compressing memory on the fly and massively reduced swapping.
As with compache, I wanted to nicely integrate the solution into the Ubuntu Upstart deamon – hence this short article. After a couple of minutes of playing the configuration was ready.
Create file zramswap.conf
in /etc/init
and put the following content in it.
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 echo 1073741824 > /sys/block/zram0/disksize echo 1073741824 > /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
Now you can start the service with sudo start zramswap
(it will be automatically started on after the reboot as well).
You will benefit from 2x1GB swap files, which will be compressed and stored in the RAM. Tested on Ubuntu 11.04.
Thanks for this. Saw the lack of support in initramfs-tools (https://bugs.launchpad.net/ubuntu/+source/initramfs-tools/+bug/665785 – also missing in 11.04) but this will give an elegant solution.
Love you man! Been around for a whole looking for this solution and it’s so simple!
Thanx a lot!
Wow, thanks for the great article! Such a clean and simple solution!
I’ve made your script automatically adapt to the amount of memory and number of CPUs in the system, here’s the link if you are interested:
https://code.launchpad.net/~elementaryos/elementaryos/zramswap-enabler
It’s likely to be enabled by default in elementary OS.
You missed a Dash in the URL
https://code.launchpad.net/~elementary-os/elementaryos/zramswap-enabler
[…] https://weirdfellow.wordpress.com/2011/05/04/compressed-ram-with-zram/ […]
[…] https://weirdfellow.wordpress.com/2011/05/04/compressed-ram-with-zram/ […]
[…] Compressed RAM with zRam […]
[…] https://weirdfellow.wordpress.com/2011/05/04/compressed-ram-with-zram/ […]
I have instructions for Fedora users here – http://mystilleef.blogspot.com/2011/10/enable-zram-in-fedora.html
[…] réellement utiliser un disque ZRAM sur votre netbook, utiliser plutôt le script fourni sur weirdfellow.wordpress.com, en l’adaptant à votre configuration Pour la taille totale compter entre 15% à 25% de la […]
i have 4GB of RAM. it’s recomended to use this script?
Thanks
It all depends how much memory you are using at the moment. But when you ask, you probably don’t need it.
Most distributions swap to disk anyway even there’s a huge amount of RAM. So, using this would make that faster, but on 4Gb I’d rather decrease swappiness. Here’s how to do it: https://help.ubuntu.com/community/SwapFaq
Since zram has been already integrated in the Linux kernel for a while now, am wondering whether the above zramswap.conf can be placed in /etc/init.d (or maybe one or more specific /etc/rcX.d’s) to successfully run for users of the upstream Debian Testing or Stable????
The given config file is intended to upstart – an event-base init daemon. If Debian is using the same, it could be used exactly as is. In Ubuntu the
/etc/init.d
is used for compatibility.Swap to compressed ramdisk – seems useless but apparently not.
It is not useless, especially when you deal with huge memory eaters like virtual machines. Reduction in swap to hdd activity and responsiveness boost is substantial.
“Swap to compressed ramdisk – seems useless but apparently not”
That is my reaction as well, though I might have said it nicer. You want to make *less* ram available to your applications by creating a swap disk in memory? Almost certainly this makes the system swap even more to disk (assuming you still have configured some disk for swap space).
If you have a lot of ram then you’re better off not having any swap at all than using this idea.
There are priorities assigned to the swaps to tell the os which swap to write to. In the script here the real, HDD based, swap is not used until zram swaps are full.
In the end you get much less swapping to HDD. But I agree – total amount of swapping operation (RAM + HDD) increased.
Nope, not “useless” at all. Very very useful, in fact. Made my netbook usable even. “Snappy” could be the correct term, even.
(For some reason, my SSD is ultra-slow, specially when it comes to swapping.)
Probably because it allows either reading or writing at a given moment, but not both at the same time, and the time required to switch is quite large.
[…] Le script est téléchargeable ici pour n’importe quel Linux (et les explications sont ici) […]
Can zram be used in conjunction with hibernate (suspend to disk) ?
I guess it should be pretty easy to test it if you have suspend to disk working now 🙂
[…] I’m gonna keep it this time ), so I let you decide that. But after considering Sergey and few others who’ve used it… it seems worth […]
[…] I’m gonna keep it this time ), so I let you decide that. But after considering Sergey and few others who’ve used it… it seems worth […]
[…] ext4 zram device and mount it as /tmp than to use uncompressed memory. Here is complete guide: Compressing RAM with zRam You need to replace mkswap+swapon with proper mkfs+mount command. Desktop: Intel Q8300@3,3Ghz, […]
[…] compressed ram in linux: https://weirdfellow.wordpress.com/2011/05/04/compressed-ram-with-zram/ […]
I installed zramswap-enabler on Ubuntu 11.10 using the ppa but when installing programs or running system upgrades i get this error:
Setting up zramswap-enabler (0.2.1-0~12~oneiric1) … start: Job failed to start invoke-rc.d: initscript zramswap, action “start” failed. dpkg: error processing zramswap-enabler (–configure): subprocess installed post-installation script returned error exit status 1 No apport report written because MaxReports is reached already Errors were encountered while processing: zramswap-enabler E: Sub-process /usr/bin/dpkg returned an error code (1)
Please let me know how to fix it.
Please use a newer version, I think 0.2.1-0~18~oneiric1 should be available already.
In Ubuntu 12.04, change num_devices in line 9 to zram_num_devices
seems that you cannot stop the zram-enable using sudo stop zramswap
The script just hangs there. IF then you press CTRL+C and launch the stop command again:
“stop: Job has already been stopped: zramswap”
using 11.04. What is wrong in the script ?
If the stop is not working, then zram-enabler cannot be updated simply using apt-get upgrade as it will hang there as well
Run “sudo start zramswap” and it will unfreeze. This is fixed in revision 21, but you have to use the unfreezing trick to upgrade to it.
Thank you for that, updated now.
[…] https://weirdfellow.wordpress.com/2011/05/04/compressed-ram-with-zram/ Questo articolo è stato pubblicato in Ubuntu Linux da fcmyworld . Aggiungi il permalink ai segnalibri. […]
[…] Home compacache Home Zram […]
[…] https://code.google.com/r/bao3cn-compcache-zram/source/browse/README และ https://weirdfellow.wordpress.com/2011/05/04/compressed-ram-with-zram และสคริปจาก […]
[…] https://weirdfellow.wordpress.com/2011/05/04/compressed-ram-with-zram/ […]
[…] https://weirdfellow.wordpress.com/2011/05/04/compressed-ram-with-zram/ […]
[…] Today I found an excellent blog post about it. https://weirdfellow.wordpress.com/2011/05/04/compressed-ram-with-zram/ […]
[…] Today I found an excellent blog post about it. https://weirdfellow.wordpress.com/2011/05/04/compressed-ram-with-zram/ […]