What is swap?
Swap space is a generic term for disk storage used to increase the amount of apparent memory available on the system. The operating system kernel moves the unused memory of program(s) to the swap file or partition which is located to the disk storage so the memory can be used for another purpose. Please note that the swap file writing and reading is very slow.

The Raspbian GNU/Linux has a 100 MB swap file loaded by default, this can wear the microSD card reducing it’s read and write cycle. You may want to disable the swap file or just move it to a USB SATA Hard Disk Drive or just move the entire Operating system to a HDD if you have enough power.

Moving the swap file:
Insert your USB HDD and mount it with fstab

sudo lsblk

NAME           MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
sda              8:0    0 31.9G  0 disk  
└─sda1           8:1    0   31.9G  0 part  /media/HDD

sudo nano /etc/fstab
#                
/dev/sda1 /media/HDD               ext4    errors=remount-ro 0       1

Editing the dphys-swapfile modify the CONF_SWAPFILE and CONF_SWAPSIZE
sudo nano /etc/dphys-swapfile

# where we want the swapfile to be, this is the default
#CONF_SWAPFILE=/var/swap
CONF_SWAPFILE=/media/HDD/swap

# set size to absolute value, leaving empty (default) then uses computed value
#   you most likely don't want this, unless you have an special disk situation
CONF_SWAPSIZE=1024

Creating new swap file and activating it.
We need to disable and delete the old swap file


sudo dphys-swapfile swapoff
sudo rm /var/swap

Generating new swap file on new path


sudo dd if=/dev/zero of=/media/HDD/swap bs=1M count=1024
sudo dphys-setup
sudo dphys-swapon /media/HDD/swap

Except where otherwise noted, this work is licensed under Creative Commons Attribution-ShareAlike 4.0 International License (http://creativecommons.org/licenses/by-sa/4.0/).
I hope that this post is useful to you, if you liked this post you may support me via liberapay. Thank you for your support.

Donate using Liberapay