Hardware How-To Technical

My Raspberry Pi needs a Swap

About Swap: Linux divides its physical RAM (random access memory) into chucks of memory called pages. Swapping is the process whereby a page of memory is copied to the preconfigured space on the hard disk, called swap space, to free up that page of memory. The combined sizes of the physical memory and the swap space is the amount of virtual memory available.

Swapping is necessary for two important reasons. First, when the system requires more memory than is physically available, the kernel swaps out less used pages and gives memory to the current application (process) that needs the memory immediately. Second, a significant number of the pages used by an application during its startup phase may only be used for initialization and then never used again. The system can swap out those pages and free the memory for other applications or even for the disk cache.

Adding Swap: File Method
I am using this method over a drive partition simply because i didn’t create a partition to use.

1 – Locate an area on disk to place the swap file. In my Raspberry Pi setup I am going to use /root
2 – Use the following dd command example creates a swap file with the name “swap” under /root directory: # dd if=/dev/zero of=/root/swap bs=1M count=512
3 – Change the permission of the swap file so that only root can access it: # chmod 600 /root/swap
4 – Make this file as a swap file using mkswap command: # mkswap /root/swap
5 – Enable the newly created swapfile: # swapon /root/swap

Your done. But wait! I don’t want to turn the swap on each time I reboot, that’s just silly.

To make this swap file available after the reboot, add the following line to the /etc/fstab file:
/root/swap               swap                    swap    defaults        0 0

Now hopefully my Raspberry Pi will be a little less prone to locking up due to being out of memory.