Press ESC to close

Raspberry Pi: How to increase SWAP partition

Hello!

Raspberry Pi is a popular platform for IoT projects, IT education and many other applications. Despite their versatility, these devices often have a limited amount of RAM. This can lead to performance problems, especially if you are running memory-intensive applications.

Operating systems on the Raspberry Pi use a SWAP partition, extending RAM with a hard drive. However, the default SWAP settings may not be sufficient, especially when working with resource-intensive applications.

In this article, I will discuss how to increase the SWAP partition to improve system performance and ensure smooth application operation even in situations where the amount of available RAM is limited. We will review the steps needed to resize a SWAP partition and the best practices involved in configuring it.

Introduction

What is a Raspberry Pi?

Raspberry Pi, Logo

Raspberry Pi is a family of single-board computers (SBCs) created by the Raspberry Pi Foundation, a non-profit organization based in the United Kingdom. The first Raspberry Pi model appeared on the market in 2012. Since then, the series has continued to evolve, delivering newer and more advanced capabilities at an affordable price.

The Raspberry Pi is used for a variety of purposes, from educational to hobbyist to professional, as an inexpensive and compact computer. It takes the form of a small board with a built-in processor, RAM, input/output (GPIO) ports, USB ports, an Ethernet port, and often with built-in Wi-Fi and Bluetooth modules.

Although the Raspberry Pi is not equivalent to traditional desktop computers in terms of performance, it offers enough computing power for a wide range of applications, such as learning programming, creating IoT (Internet of Things) projects, home servers, monitoring systems and more.

All Raspberry Pi models run on open-source software, usually based on a Linux operating system such as Raspberry Pi OS (formerly known as Raspbian), although other operating systems such as Windows 10 IoT Core can also be run on them. Thanks to its popularity, the Raspberry Pi has become an essential tool for hobbyists, students, developers and even some professionals in various fields of computer science and electronics.

What is a SWAP partition?

Raspberry Pi SWAP, DiskSWAP, also known as a swap partition, is an area on the hard drive that the operating system uses as additional memory when RAM is insufficient to support currently running processes. The main purpose of SWAP is to ensure that the system continues to operate when RAM is exhausted or when dealing with large amounts of data.

When the operating system finds that RAM is running out and new processes require more memory, it uses SWAP as a place to store temporary data. This means that some data from RAM that is not currently being used is moved to the hard disk to make room for new data. When this data is needed again, the operating system moves it back to RAM.

SWAP is especially useful in situations where you are running multiple applications simultaneously or working with large files that require a lot of memory. However, using SWAP can cause the system to slow down, as accessing data on the hard disk is much slower than data stored in RAM. Therefore, it is recommended that SWAP be used as a last resort when RAM is almost completely used up.

For Linux-based systems, including the Raspberry Pi, the SWAP partition is usually created during the installation of the operating system, but it can also be customized by changing its size according to the user’s needs.

Preparation

Before we proceed with the configuration, it is a good idea to properly prepare our device – I will use Raspberry Pi version 5 to create the tutorial. We will issue commands to our device via SSH.

  • I recommend using rpilocator.com to see where you can currently get a reasonably priced device;
  • For Microsoft Windows operating system I recommend PuTTY, while for Apple macOS operating systems I recommend Termius.

Step 1 – Configuration on Raspberry Pi

Update package list and upload updates

  • Launch PuTTY (Windows OS) or Termius (macOS), then connect to your device,
  • Enter the following command to update the package list and install available updates:
sudo apt-get update && sudo apt-get upgrade -y

Check the current size of SWAP

Run the free -h command in a terminal to check the current SWAP size on your Raspberry Pi. This will display the amount of SWAP memory in use and available.

free -h
               total        used        free      shared  buff/cache   available
Mem:           7.9Gi       1.6Gi       3.6Gi        86Mi       2.8Gi       6.3Gi
Swap:           99Mi          0B        99Mi

Turn off SWAP

Before you start changing the SWAP partition on your Raspberry Pi, make sure you don’t use any RAM-intensive applications.

  • First, turn off SWAP so you can resize it. In the terminal, enter the command.
sudo dphys-swapfile swapoff

Resize the SWAP partition

  • To change the size of the SWAP partition, you need to edit the dphys-swapfile configuration file. Launch a text editor, such as nano:
sudo nano /etc/dphys-swapfile

Modify SWAP partition settings

  • Find the line with CONF_SWAPSIZE= .
  • By default, this is set to 100.
  • Modify this value to the desired SWAP partition size, for example, 1024 for 1GB SWAP:
# /etc/dphys-swapfile - user settings for dphys-swapfile package
# author Neil Franklin, last modification 2010.05.05
# copyright ETH Zuerich Physics Departement
#   use under either modified/non-advertising BSD or GPL license

# this file is sourced with . so full normal sh syntax applies

# the default settings are added as commented out CONF_*=* lines


# where we want the swapfile to be, this is the default
#CONF_SWAPFILE=/var/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

# set size to computed value, this times RAM size, dynamically adapts,
#   guarantees that there is enough swap without wasting disk space on excess
#CONF_SWAPFACTOR=2

# restrict size (computed and absolute!) to maximally this limit
#   can be set to empty for no limit, but beware of filled partitions!
#   this is/was a (outdated?) 32bit kernel limit (in MBytes), do not overrun it
#   but is also sensible on 64bit to prevent filling /var or even / partition
#CONF_MAXSWAP=2048
  • Save your changes and close the editor: In nano, press Ctrl + X, then confirm your changes by pressing Y and then Enter.

Re-initialization

  • We can now reinitialize the exchange file on the Raspberry Pi by running the following command.
  • Performing this operation will delete the original replacement file, after which it will be re-created to match the newly defined size.
sudo dphys-swapfile setup

SWAP startup

  • Restart the SWAP service to apply the new configuration:
sudo dphys-swapfile swapon
  • Check the new SWAP size: Again, use the free -h command to make sure SWAP has been increased as you expected.
  • If you need – The easiest way to reload all programs with access to the new memory pool is to reboot the device.
  • Done! Enjoy your use! 🚀

If you have additional questions about the setup, go ahead and leave a comment under this article or contact me directly. I will be happy to answer any concerns and help solve any problems. Your questions can help improve this guide for other users.

Additional materials and information

For further exploration and more information, I recommend checking out the links below. They are valuable sources that were used in the development of this guide.

Filip Chochół

Filip Chochol runs two blogs: personal “chochol.io” and together with his girlfriend “Warsaw Travelers” about travel. He specializes in IT resource management and technical support, and has been active in the field of cyber security awareness for almost two years. A proponent of open-source technologies, he previously worked in the film and television industry in the camera division (2013-2021). After hours, he develops interests in smart homes and networking.

Leave a Reply

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.