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.

Assembled Gertboard available at Farnell – #RaspberryPi @Raspberry_Pi

GERTBOARD - GERTBOARD - BOARD, GERTBOARD ASSEMBLED | CPC

Exciting news from Farnell (revealed in a tweet, and then on Scoop.it) – the assembled Gertboard is now available, and it’s not as expensive as I thought it would be. £30 + VAT (£36) buys you the board. They’re also selling the Pi with a case for £31.50 (inc VAT). You will obviously have to pay postage on top, but that’s a pretty good deal.

Gordon over at Drogon sums the Gertboard up nicely (in fact I had to search long and hard for something as succinct as this!):

The Gertboard is an add-on GPIO expansion and experimenter board for the Raspberry Picomputer. It comes with a large variety of components, including buttons, LEDs, A/D and D/A converters, a motor controller, and an ATmel ATmegs 328p AVR microcontroller which you can program using the standard Arduino IDE (with some minor modifications). Read more

To order an assembled Gertboard, follow this link: GERTBOARD