Saturday, March 30, 2013

Configuring swap space in a Raspberry Pi

Recently I found myself needing to increase the amount of swap space available in my Raspberry Pi. My Pi runs a Raspbian (Wheezy), and I was trying to compile phantomjs because none of the available binaries seem to work for me:

https://github.com/piksel/phantomjs-raspberrypi
https://github.com/aeberhardo/phantomjs-linux-armv6l

Aeberhardo's project provides instructions on how to compile your own phantomjs in your Pi. Anyway, one of the compilation steps was failing and I noticed the problem was my Pi was running out of memory. Initially it is configured to use 100Mb of swap space, and I needed more.

Raspberry Pi uses a disk based swap solution called dphys-swapfile . To configure the swap space you just have to edit the file /etc/dphys-swapfile:

CONF_SWAPSIZE=[size in mb]

and restart the swap daemon:

/etc/init.d/dphys-swapfile stop
sudo /etc/init.d/dphys-swapfile start

There you go, now you are all set with an expanded (or reduced) swap space. Be aware that swap on the Pi is tremendously slow, and that it reduces the life of the SD card, since SD cards have a limited number of write operations that can be performed on them before they stop working.

I hope this helps!

Enjoy your Raspberry Pi!!!