Use #RaspberryPi RAM as swap space

This is interesting. Someone over at Linux Journal is using “zram” to turn half the Pi’s RAM into a swap space. They even claim to have removed the swap file entirely, which should improve the life of your SD cards.

Most of the work was done using a script from (which isn’t always up). I’ve included the complete post text here just in case they both disappear!


SD cards are getting better every year when it comes to wear leveling and write cycles. The concern is always that excessive use of the SD card will wear it out, and it will become read-only. Because the RPi also suffers from limited and non-expandable RAM, the issue of swap space is highly debated.

Raspbian comes installed by default with a swap file active—not a swap partition, but a swap file. Whether this swap file will wear out your SD card is still up for debate. I tend to be paranoid about such things, so I searched for an alternative. Because I have the 512MB units, I have a little more RAM to work with, so I decided to use zRam to compress some of the system RAM and use it as swap space. How does that work? Basically, zRam allows the system to use RAM as swap space, but in a compressed mode that allows it to appear as more RAM+swap than the system actually contains.

zRam already is compiled into the Raspbian kernel, so if you want to try my method, it just takes a little scripting. I absolutely love the script I found at the Web site. That site is off-line at the time of this writing, so I’ve included the script below. Create a file called /etc/init.d/zram, which contains the following:

#Provides: zram
#Default-Start: 2 3 4 5
#Default-Stop: 0 1 6
#Short-Description: Increased Performance In Linux With zRam 
#(Virtual Swap Compressed in RAM)
#Description: Adapted for Raspbian (Rasberry pi) by 

start() {
    mem_total_kb=$(grep MemTotal /proc/meminfo | grep -E --only-matching '[[:digit:]]+')

    modprobe zram

    sleep 1
    #only using 50% of system memory, comment the line 
    #below to use 100% of system memory

    echo $((mem_total_kb * 1024)) > /sys/block/zram0/disksize

    mkswap /dev/zram0

    swapon -p 100 /dev/zram0

stop() {
    swapoff /dev/zram0
    sleep 1
    rmmod zram

case "$1" in
        sleep 3
        echo "Usage: $0 {start|stop|restart}"

Then you need to make the init script executable and force it to start automatically. The following commands accomplish that:

sudo chmod +x /etc/init.d/zram
sudo update-rc.d zram defaults

Basically, as written, the script will use half the available RAM as compressed swap space. Again, all credit goes to the folks at for creating the init script. I’ve been very happy with the performance, and it enabled me to get rid of the swap file altogether.

Hopefully someone will find this information useful. Here’s a link to the article at Linux Journal.

Your First Bite of Raspberry Pi | Linux Journal.

6 comments for “Use #RaspberryPi RAM as swap space

  1. 22 January 2013 at 8:48 pm

    Hmmm its an interesting idea but I’m not convinced. You end up sacrificing half your ram (which is scarce anyway) and also using up valuable CPU cycles to save an SD card which by the time it actually dies will only cost a few pence to replace?

    I guess it depends on what you are doing with your Pi, if memory and CPU aren’t an issue but you want to run 24×7 for years I guess it has an application?

    • 23 January 2013 at 6:20 am

      Oh, I’m not convinced either. 512mb is very little to play with anyway, what with sharing it between main and graphics. Just found it interesting that you _could_ do it. Would love to see some benchmarks, though, if only for media centre Pis.

  2. 5 February 2013 at 2:34 pm

    I tried it and tested FPS with OpenCV webcam viewing in a simple QT window. FPS before zram: 29.4 After zram: 15.6. So now I have to figure out how to remove it again. 🙂

    • 5 February 2013 at 2:55 pm

      Ew, that’s not good!

    • 25 May 2013 at 10:02 pm

      Thank you for your test! 🙂

      sudo /etc/init.d/zram stop

  3. 9 April 2014 at 5:26 pm

    Wonderful idea, it worked for me also. Thanks with regards

Leave a Reply

Your email address will not be published. Required fields are marked *