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

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 We'd love to see what you're all up to.


Shawn is Associate Editor here at Linux Journal, and has been around Linux since the beginning. He has a passion for open source, and he loves to teach. He also drinks too much coffee, which often shows in his writing.


Comment viewing options

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


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

CK's picture
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!


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?

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 !!


olivery's picture

The observation or line!

Electrical current'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.


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

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.


عين العرب's picture

thank you about this blog
arab eye