Adding Swap Memory to the Beaglebone Black

Reading Time: 2 minutes

I recently needed to build Python 3.6 from source on the BeagleBone Black for a robotics project and discovered that the build would always fail after running out of memory. Who could have figured that 512MB of RAM wasn’t enough to build Python from source?! While I could have set up cross-compilation and performed the heavy lifting on my computer with more resources, I figured this was a perfect problem to solve by adding swap memory to the BeagleBone Black.

What is a swap space?

At a very high level, a swap space (usually in the form of a partition) allows the system to move some of the “lower priority” memory pages (blocks of memory) to the partition or file so that higher priority tasks can use the memory. The combined size of the physical memory and swap space is called virtual memory. Gary Sims has a great primer on swap spaces over at

In this case, the BeagleBone Black only has 512MB of RAM so I’ll be adding ~1GB of swap space to my SD card in the form of a swap file. This will allow it to perform more memory-intensive tasks without running out of memory (at a cost of slower access times).

Creating a swap file

Creating a swap space is a fairly simple process that involves creating a file (and filling it with zeros) then telling the system to use that file as a swap space. Finally, the system has to be configured to load that file on startup.

Check if you already have a swap file
sudo swapon -s

In my case I already created the file so it’s shown. The assumptions is that you’re performing these steps because you don’t have a swap space.

Create the swap file

Use the dd utility to create a file with a fixed size and fill it with “zeroes”. In this case I’m moving 1000 “zeroes” from /dev/zero in block sizes of 1024k (1MB) into my file at /var/cache/swap/swapfile. This will result in a swap file of around 1 gigabyte. Keep in mind that the swap file is actually named “swapfile” with no extension.

sudo dd if=/dev/zero of=/var/cache/swap/swapfile bs=1024k count=1000

use chmod to change the file permissions of the new swap file.

sudo chmod 0600 /var/cache/swap/swapfile

Use the mkswap  command to create the swap area using our newly created swap file.

sudo mkswap /var/cache/swap/swapfile

Finally, enable the swap area with the swapon command

sudo swapon /var/cache/swap/swapfile
Configure the swap file to automatically mount on boot

Open up the /etc/fstab file with the nano editor.

sudo nano /etc/fstab

Append the following line to the file:

/var/cache/swap/swapfile    none   swap   sw   0   0

That is all there is to it for this specific case – adding swap memory to the BeagleBone Black is pretty straightforward!

The newly created swap space being heavily during building

One thought on “Adding Swap Memory to the Beaglebone Black”

Leave a Reply

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