Your First Bite of Raspberry Pi

To Swap or Not to Swap

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.

What's Next?

Hopefully this intro to the Raspberry Pi will make the device a little less intimidating for folks like me who were hesitant. With its ability to run XBMC, function as a complete desktop system and yet barely sip electricity, the RPi is a great device for countless projects. And at $35, it's absolutely dirt cheap.

We expect to see many, many Raspberry Pi projects around the Linux Journal offices. Hopefully you'll see a few around your place as well. If you have any particularly cool projects or case mods, send a photo to ljeditor@linuxjournal.com. We'd love to see what you're all up to.

______________________

Shawn Powers is an Associate Editor for Linux Journal. You might find him chatting on the IRC channel, or Twitter

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

raspberry

galant's picture

I have a raspberry pi and a old screen from my phone and plenty other screens from other devices and some random screens. Could someone give me a link or instructions on how to do so. spelmaskiner

Anne, best wishes from the

ma's picture

Anne, best wishes from the Corporate Email SolutionJust wanted to drop a comment and say I am new to your blog and really like what I am reading. Thanks for the great content. Look forward to coming back for more......

I like your point which you

Debora's picture

I like your point which you given in your blogs specially Case Option: Buy One, Case Option: Steal from the Nursery, Case Option: 3-D Printing, Case Option: Regular Printing. http://www.usacoverage.com

http://www.lovewigs88.com You

CK's picture

http://www.lovewigs88.com
Your blog is a great one. What really impresses me is that you are correctly mentioned that there are thousands of tools that are available to create a website or launch one but what matters is that you choose the right one, the one that gives you all that is actually needed

One question about linux in

anony's picture

One question about linux in Modelos de Currículo

first bite

roger gull's picture

Here in dotMobi we found ourselves repeatedly switching UAs and reloading pages in our testing of our own products lipo 6 black, lipo 6 unlimited and dhea

No, better check this out

Andrey's picture

No, better check this out smscredit this 100 more better then your page, lol!

raspberry

Andreole's picture

The van has always been difficult to start but when I asked a mechanic I was told some vehicles are just like that lipo 6 black ultra

good sd card?

miltonhonsten's picture

Seems like a nice little toy, but I don't really like the fact that it does not boot from USB. Can you recommend a good SD-Card?
http://www.comedians-in-deutschland.de

is that usb card come inside

dennishobson's picture

is that usb card come inside an cereal box ? ... i really think that they sould put those in brasilian cereal ... i would be so happy ahahah

have a nice day !!

zadi

olivery's picture

The observation or line!

Electrical current

absolutely.geek.nz's picture

Electrical current and therefore power supply capacity is measured in Amps or mA.

Power supplies are rated in Watts which is V x A = W, there is no unit of time involved.

Energy has a time component W x s = Joules.

If you consider a battery is rated at a Voltage it is able to provide then a simplification of the power that it can supply is in Amp-Hours or Ah / mAh. Power supplies by definition do not have an Ah rating as they don't run out....execpt in a power cut etc.

missie

olivery's picture

in the line, in the horizont, in the tv online

Can you help me?

Anonymous's picture

Do you think you could perhaps make a simpler explanation on the steps to split the brain to allocate the preffered RAM to GPU

Swap on a network filesystem

Brian Likosar's picture

Have you given any thought/consideration to swapping on NFS or the like? I wonder if that would address your concerns about limited memory...

Dear Shawn, Please note that

Janne's picture

Dear Shawn,

Please note that current is not measured in mAh but in mAh/h, or just mA. But who cares?

Best regards
/Janne

Power and current

Anonymous's picture

But who cares?

I care, Janne, because I have a selection of power supplies that can deliver this voltage, but would like to know the max. current expected before fitting one. Why blow a functioning PS unnecessarily.

test

عين العرب's picture

thank you about this blog
arab eye

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix