When building large projects with Yocto/Bitbake I encountered hangups of my host OS due to lack of RAM space.

The solution to that was creating a SWAP partition, which actually allocates space on a SSD (preferably) or a HDD. Note that a HDD and SSD are always slower than RAM, but better than having hangups.

How to create a swapfile on Ubuntu 18.04 (may work on other distros as well). Replace <user> with your local user (echo $USER).

  1. Create a swap file with dd. I generally double my RAM capacity, so I choose my swapfile to be the same size as RAM on my system.
# bs=1G and count=4 -> 4GB swapfile
$ dd if=/dev/zero of=/home/<user>/swapfile bs=1G count=4

2. Set correct permission on the swapfile

$ sudo chmod 600 /home/<user>/swapfile

3. Make ‘swapfile’ usable as swap

$ sudo mkswap /home/<user>/swapfile

4. Tell the OS to use the swapfile

$ sudo swapon /home/<user>/swapfile

In principle you are done. The OS is using your swapfile. However it is not acitivated on a restart of your OS. To fix that we have to add it to the fstab file.

5. Add the following line to the /etc/fstab

/home/<user>/swapfile none swap sw 0 0

Check with “top”, “htop”, or even “bashtop” your new physical memory size!

Finally, you’d like not to use the swapfile if it is not necessary. You can specify the “swappiness” for that.

# 0 = disable swap, 100 = swap as much as possible
$ sysctl vm.swappiness=10

Sources:
[1] https://help.ubuntu.com/community/SwapFaq