Loading...

Enable swap for Raspberry Pi

When you migrate your root to an external usb hdd, the next step is to enable swap.

Let’s see the used and free memory with free -h

root@pelion:/boot# free -h
             total       used       free     shared    buffers     cached
Mem:          973M       858M       115M         0B        28M       703M
Swap:           0B         0B         0B

As you can see there is no swap available. Since the previous root was on the SD card, it is a bad idea for swapping, as result the micro SD card will wear out. Using an external usb hdd allows us to use swap on the hdd, if the Raspberry Pi needs to swap. Raspian uses a script dphys-swapfile to manage swap. The configuration file is located at /etc/dphys-swapfile. Raspian doesn’t need a separate swap partition.

CONF_SWAPSIZE=2048

My configuration allows a 2 GB swap file. By default the swap file is /var/swap.

Show active swap with summary option

root@pelion:~# swapon -s
Filename                                Type            Size    Used    Priority
/var/swap                               file            2097148 0       -1

To deactivate swap and activate swap again

swapoff /var/swap
swapon /var/swap

Usually the Raspberry Pi won’t need swapping, but in case it needs to, it is capable now.