Set Raspberry PI Swap Memory


Last Updated on 2nd September 2023 by peppe8o

In this tutorial I’m going to show you how to set Raspberry PI swap memory.

Beside CPU, RAM is the most valuable resource in every computer. It is where data are stored for running programs and it is one the most important resource for applications managing a big amount of data. Raspberry PI Swap Memory, like other linux systems, can reduce small RAM impact avoiding Out of Memory errors

Swap memory is a file on computer storage dedicated to help RAM when this is not enought according to programs need. It is a common feaure for all unix like systems. It has a very important role on computer stability and performance, as swap memory is used every time your physical RAM is too small compared to what programs are requiring to correctly manage their data.

Swap Memory Role in Computer

Common systems have 2 main kind of resource: processing resources and memory resources.

Processing resources are strictly related to CPU (Central Processing Unit) and it is strictly based on what kind of processor you have. The only way to work on this side of performance factors, excluding CPU substitution, is overclocking. This means elevating the number of instruction that CPU can work each second. But this practice is dangerous for your CPU, untill you really know what you are doing.

On the other side, data memory resources can be roughly classified on RAM and storage.

RAM usually is the fastest memory resource available for every computer. For its performance, all programs try to occupy this memory in order to get results in less time. RAM is also cleaned by OS at every shutdown, as it can store data only if computer is powered on.

Storage resources are Hard Disk Drives, Solid State Drives, USB sticks or MicroSD cards (in Raspberry PI case). They are far slower compared to RAM, but they store data also after computer shutdown. This resource is commonly used by programs to store data once they are consolidated according to code jobs.

For their technology, RAM costs are higher than storage.

The common problem is that each program isn’t aware of how much RAM is available between OS and other concurrent programs. This can lead to “Out of Memory” errors when total RAM amount required from a system is greater than phisical. This is also a common problem for programs managing high data amounts (like image or video processing aplications).

For this reason, linux systems use portion of their storage merged to RAM, making programs not aware that they are using storage instead of RAM. This solution can reduce program speed but will avoid memory filling and program interruptions.

In this guide I will show you how to set Swam Memory on a Raspberry PI 3 Model A+, but this guide applies to all Raspberry PI boards.

What We Need

RPI 3 model A+

As usual, I suggest adding from now to your favourite e-commerce shopping cart all the needed hardware, so that at the end you will be able to evaluate overall costs and decide if to continue with the project or remove them from the shopping cart. So, hardware will be only:

Check hardware prices with following links:

Amazon raspberry pi boards box
Amazon Micro SD box
Amazon Raspberry PI Power Supply box

Step-by-Step Procedure

Prepare Operating System

If not already done, please install Raspberry Pi OS Lite (for a fast, headless OS) or Raspberry PI OS Desktop (in this case, working from its internal terminal).

Make your OS up to date. From terminal:

sudo apt update -y && sudo apt upgrade -y

Check Current Swap Memory

Current swap size and position can be verified in several ways. Before going on, consider that swap size is usually calculated in MByte. Tools reporting this value in bits will consider that, for memory, 1 Kbyte is equal to 1028 MByte

The very first way where to get current swap size, is using debian “free” command:

pi@raspberrypi:~ $ free
               total        used        free      shared  buff/cache   available
 Mem:         374964       36292      224816        2708      113856      283268
 Swap:        102396           0      102396

Last row shows current swap total space in kilobytes, divided between total, used and free.

Another common way, is using “top” command (CTRL+C to exit):

raspberry pi 3 model A+ top

Top is a useful tool because it shows, beside swap memory info on 5th row, system and running processes details in real time.

Last but not least, the tool I personally love: htop (F10 key to exit):

raspberry pi 3 model A+ htop

What makes htop different is that you can manage this window also with your mouse. For example, try clicking on “MEM%”. You will see processes sorted by memory usage. Swap memory usage is traced in first part of this window (“Swp”).

Set Raspberry PI Swap Memory Size

Changing swap size requires disabling swap, editing size, enabling swap and restarting swap service. All of these actions are done with following terminal commands:

Disable swap:

sudo dphys-swapfile swapoff

Change swap size in dphys-swapfile. Open this file for editing:

sudo nano /etc/dphys-swapfile

Identify CONF_SWAPSIZE parameter and change according to your needs. For example, I will change it to:


Exit and save. Enable swap and restart dphys service:

sudo dphys-swapfile swapon
sudo systemctl restart dphys-swapfile.service

Check that swap has changed value:

pi@raspberrypi:~ $ free
               total        used        free      shared  buff/cache   available
 Mem:         374964       36648      223092        2708      115224      282916
 Swap:        511996           0      511996

This will persist also after Raspberry PI reboot.

Set Swappiness

Swappiness is a concept complementary to swap memory. It drives when linux kernel should start using swap file instead of RAM. This concept is a bit more complex of what some people believe.

When linux kernel needs more phisical memory, it can decide to use page cache (also stored on disk) or moving pages to swap memory. Swappiness defines how kernel will be likely to use swap instead of RAM. So this value doesn’t set swap usage vs RAM. A good explaination is available on stackexchange from Stephen Kitt.

Current swappiness value can be verified with vm.swappiness command:

pi@raspberrypi:~ $ sysctl vm.swappiness 
vm.swappiness = 60

To change swappiness, use following command, setting swappiness value between 0 and 100:

sudo sysctl -w vm.swappiness=50

This change will be temporary and lasts until Raspberry PI reboots. To make it permanent, you will need to edit sysctl.conf file:

sudo nano /etc/sysctl.conf

and append at the end of file:


Exit and save.


How useful was this post?

Click on a star to rate it anonymously!

Average rating 4.6 / 5. Vote count: 9

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?