Optimal swap size for building with Yocto Project

From KoanSoftware Wiki
Revision as of 17:26, 25 March 2025 by Koan (talk | contribs) (Created page with "== Optimal swap size for building with Yocto Project == BitBake, the build engine used in the Yocto Project, is notorious for its appetite for system memory, especially when...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Optimal swap size for building with Yocto Project

BitBake, the build engine used in the Yocto Project, is notorious for its appetite for system memory, especially when compiling large and complex software images. When your computer's RAM gets filled to capacity during a BitBake process, it can lead to frustrating build failures, system crashes, or agonizingly slow build times. This is where swap space comes in.

Think of swap space as a reserve of extra memory on your hard drive. When your system's RAM is fully occupied, the operating system cleverly moves less frequently used data from RAM to this swap space. This frees up crucial RAM for BitBake to continue its work, preventing those dreaded out-of-memory errors that halt progress.

While using swap space introduces a performance penalty, as hard drives are significantly slower than RAM, it provides a crucial safety net. It allows builds that would otherwise crash to complete successfully. Essentially, it trades a bit of speed for increased stability and reliability. This improvement is a practical way to address memory limitations and ensure that BitBake can handle even the most demanding build tasks, ultimately making the development process smoother and more predictable.


What is swap and why resize it

Recommended swap sizes

  • System RAM : 8GB to 64GB
  • Swap size  : At least 4GB
  • Swap suggested : 1.5x RAM


Prerequisites

Before modifying your swap configuration, ensure you have:

  • Root or sudo privileges: All commands in this guide require elevated permissions
  • Sufficient free disk space: Verify available space with df -h
  • Backup of critical data: System-level changes always carry some risk
  • Understanding of current swap usage: Check with these commands:
   # Show current swap files/partitions and their sizes
   sudo swapon --show
   
   # View memory and swap usage
   free -h
   
   # Check if swap is being actively used
   vmstat 1 5


Step-by-Step process to resize swap

Turn off all running swap processes

   sudo swapoff -a

This command disables all swap spaces defined in /etc/fstab and currently active. It may take some time to complete as the system moves data from swap back to RAM.


Resize the swap file

For this example, we'll create a 16GB swap file (adjust the size to your needs):

   # Remove the old swap file if it exists
   sudo rm /swapfile
   
   # Create a new swap file with desired size (4G in this example)
   sudo fallocate -l 16GB /swapfile
   
   ### Alternative method if fallocate fails:
   ### sudo dd if=/dev/zero of=/swapfile bs=1G count=16


Set proper permissions and ownership

Swap files should be readable and writable only by root for security:

   # Set correct permissions (only root can read/write)
   sudo chmod 600 /swapfile
   
   # Verify permissions
   ls -lh /swapfile


Set up the swap area

   # Format the file as swap
   sudo mkswap /swapfile

You should see output confirming the swap file creation.


Activate the swap file

   # Enable the swap file
   sudo swapon /swapfile

Verify the swap is active

   sudo swapon --show
   free -h