Articles

Compressing RAM with zRam

In Ubuntu on 04/05/2011 by weirdfellow Tagged: , , , , , , ,

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.

About these ads

40 Responses to “Compressing RAM with zRam”

  1. 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.

  2. Love you man! Been around for a whole looking for this solution and it’s so simple!

    Thanx a lot!

  3. Wow, thanks for the great article! Such a clean and simple solution!

  4. 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.

  5. […] Compressed RAM with zRam […]

  6. I have instructions for Fedora users here – http://mystilleef.blogspot.com/2011/10/enable-zram-in-fedora.html

  7. […] 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 […]

  8. i have 4GB of RAM. it’s recomended to use this script?

    Thanks

  9. 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.

  10. 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.

  11. […] Le script est téléchargeable ici pour n’importe quel Linux (et les explications sont ici) […]

  12. Can zram be used in conjunction with hibernate (suspend to disk) ?

  13. […] 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 […]

  14. […] 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 […]

  15. […] 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, […]

  16. 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.

  17. In Ubuntu 12.04, change num_devices in line 9 to zram_num_devices

  18. 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

  19. […] http://weirdfellow.wordpress.com/2011/05/04/compressed-ram-with-zram/  Questo articolo è stato pubblicato in Ubuntu Linux da fcmyworld . Aggiungi il permalink ai segnalibri. […]

  20. […] Home compacache Home Zram […]

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

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: