Adding swap to the Raspberry Pi

Adding swap to the Raspberry Pi

Recently I was working on a project that required more RAM than my Raspberry Pi has. After all, the model B only shipped with 512MB and part of that needs to be allocated to the video card. But Linux has a method of dealing with running out of memory called the swap. The swap is a file on disk that serves as ‘overflow’ RAM space. It is slower than regular RAM but is a better solution than simply having the program crash. At the time I was using Arch Linux which doesn’t setup any swap by default. Raspbian does setup swap space but doesn’t do it in a recommended manner.

Let me put this right up front. Using swap space on SD cards and USB flash drives is not a good idea. It creates more problems than it solves. To start, it will be too slow to be useful. Second, and more importantly, Linux swapping can cause many write cycles which will tax your SD/flash drive quickly causing failures. I only use swap when I have a USB hard disk. A portable hard disk solves the speed and longevity problems quite well. I happened to have a old 6GB drive in my parts bin that I put into a portable enclosure.

The Basics

First, check how much memory you have with free -h (the -h means human readable). You should see something like:

              total        used        free      shared  buff/cache   available
Mem:           371M         16M        300M        340K         54M        286M
Swap:            0B          0B          0B

Meaning the board has 371MB of available RAM and no swap. I needed about 400MB of RAM and the application I was running crash with a core dump every time it ran.

Raspbian Swap

Raspian uses a script dphys-swapfile to manage swap. The standard image includes this turned on by default. The configuration files is located at /etc/dphys-swapfile. The only parameter in the file is CONF_SWAPSIZE=100 which creates a 100MB swapfile in /var/swap. As mentioned above, putting the swapfile in /var is not a good idea because that directory is on your SD card. You can change the file location with CONF_SWAPFILE=/your/file/location. My /etc/dphys-swapfile looks like this

CONF_SWAPSIZE=1024
CONF_SWAPFILE=/mnt/sda1/swap.file

NOTE: sda1 is my usb hard drive which is automounted.

Arch Linux Swap

Arch does not setup any swap by default. The following steps are what was needed to setup the swap space. First mount the hard drive. I attached my drive to /mnt/sda1.

Next I created the swap file. I chose to use a file rather than a full partition because it was easier at the time.

# dd if=/dev/zero of=/mnt/sda1/swap.file bs=1M count=1024

dd is a command to copy bytes from one location to another. The ‘if’ is the input file, a special /dev that generates all 00s.The ‘of’ is the output file on the hard disk. the ‘bs’ is the block size (1 MB in this case) and the count is the number of blocks – 1GB of total space. This command can take a few minutes to run.

Set the permissions so only root has access. This plugs a security hole.

# chmod 600 /mnt/sda1/swap.file

Next create a swap memory space and turn it on with these commands.

# mkswap /mnt/sda1/swap.file
# swapon /mnt/sda1/swap.file

To make the swap file available on every boot you need to add two lines to the fstab file. The first is the line for attaching your USB hard drive (not shown). The second will mount your swapfile. I added the following to my /etc/fstab

/mnt/sda1/swap.file none swap defaults 0 0

Last Thoughts

In all of my cases, the swap file was on a removable USB hard drive. Unmounting the drive will cause your system to crash if you do not turn off the swap first. Disable the swap with the following command

# swapoff /mnt/sda1/swap.file

Good luck and ask questions below.