Contents

Increase Swap on a Raspberry Pi

Introduction

The swap file is used to increase the system’s total accessible memory beyond its hardware capabilities.

This means that when all of the Raspberry Pi’s RAM is exhausted, it can start using the swap file as memory instead.

The addition of more virtual memory allows the system to deal with more memory intensive tasks without running into out of memory errors or having to shut down other additional packages.

However, the downside to this is that accessing the swap file is a significantly slower process that can create slowdowns.

The reason for this is that the swap file exists on your actual disk, which has significantly lower read and write speeds then your RAM.

Another caveat of a large swap file is that you need that space to be free on your SD Card. You can’t set a swap file on your Raspberry Pi larger than your available free space.

You will need

Increase the Swap File on a Raspberry Pi

Before we can increase our Raspberry Pi’s swap file, we must first temporarily stop it. The swap file cannot be in use while we increase it. To stop the operating system from using the current swap file, run the following command.

1
sudo dphys-swapfile swapoff

Next, we need to modify the swap file configuration file.

1
sudo vim /etc/dphys-swapfile

Within this config file, find the following line of text.

1
CONF_SWAPSIZE=100

To increase or decrease the swap file, all you need to do is modify the numerical value you find here. This number is the size of the swap in megabytes.

1
CONF_SWAPSIZE=1024

Whatever size you set, you must have that space available on your SD card. We can now re-initialize the Raspberry Pi’s swap file by running the command below.

1
sudo dphys-swapfile setup

With the swap now recreated to the newly defined size, we can now turn the swap back on.

1
sudo dphys-swapfile swapon

To reload all programs with access to the new memory pool, then restart your device.

1
sudo reboot

References