Activate Swap on Archlinux for Raspberry Pi

I’m working on a new project involving a Raspberry Pi Zero W running ArchLinux ARM. The Pi Zero W, being with only 512MB RAM is having issues performing a task I need that must be done in memory using the vendors command line app. I could rewrite the vendor’s app to be able to run on the 512MB RAM but I’m keeping that for the future. Aside this, there might be other apps that need a little more RAM. This is where swap space comes in useful.

A little primer on swap space. Swap is what is used when the physical RAM is full. If the system needs more memory resources and the RAM is full, inactive pages in memory are moved to the swap space. Swap space is located on hard drives, which have a slower access time than physical memory, thus affecting performance. The olden rule for swap was to put it into its own partition however these days there is no performance advantage to either a contiguous swap file or a partition as both are treated the same way.

The following steps is to get swap on ArchLinux ARM

  1. Create the swap file with the desired size. We can use the dd command to create the file if needed.

    fallocate -l 1G /swapfile
  2. Change the file permission to prevent unauthorized people from viewing the contents

    chmod 600 /swapfile
  3. Format the swap file to the appropriate filesystem type

    mkswap /swapfile
  4. Activate the swap file for immediate use

    swapon /swapfile
  5. To have the swap available on bootup, add it to fstab

    /swapfile none swap defaults 0 0

If there ever comes a time when you no longer need or want the swap file, perform the following steps

  1. Deactivate the swap

    swapoff /swapfile
  2. Remove the swap file

    rm -f /swapfile
  3. Remove the entry from fstab (if one exists).

Hope this helps anyone who needs a little more memory boost on the little Raspberry Pi Zero W. Cheers!