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 extremeshok.com (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 eXtremeSHOK.com 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:


#!/bin/bash
### BEGIN INIT INFO
#Provides: zram
#Required-Start:
#Required-Stop:
#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 eXtremeSHOK.com 
#using https://raw.github.com/gionn/etc/master/init.d/zram
### END INIT INFO

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
    mem_total_kb=$((mem_total_kb/2))

    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
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        sleep 3
        start
        ;;
    *)
        echo "Usage: $0 {start|stop|restart}"
        RETVAL=1
esac

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 eXtremeSHOK.com 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

Leave a Reply to recantha Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.