If you’re, like me, regularly building Qt, you probably have noticed a decent hunger for memory, especially when linking Webkit. This part of the build can take well over 8GB of RAM, and when it fails, you get to do it over again.
The unfortunate data point is that my laptop only has 4GB, which is enough for most (but one) cases. Short of buying a new laptop, here’s a trick how you can get through this part of the build: Create a swap file. Creating a swapfile increases your virtual memory. This won’t make it fast, but it at least gives Linux a chance to not run out of memory and kill the ‘ld’ process. Creating a swapfile is actually really easy under Linux, you just have to know your toolbox. Here’s the quick run-down of steps:
First, create an empty file:
fallocate -l 4096M /home/swapfile
Using the fallocate syscall (which works for newer kernels, but only for btrfs, ext4, ocfs2, and xfs filesystems), this can be done fast. In this example, I have enough space on my home partition, so I decided to put the swapfile there. It’s 4GB in size, which should be plenty of virtual memory to finish even the greediest of builds — your mileage may vary. If you’re not able to use fallocate, you’ll need a bit more patience and dd.
As your swap should never be readable by anybody else than root, change its permissions:
chmod 600 /home/swapfile
Next, “format” the swapfile:
Then, add it to your virtual memory pool:
You can now check with tools like `free` or `top` (tip: use the much nicer `htop` if you’re into fancy) that your virtual memory actually increased. Once your build is done, and you need your disk-space back, that’s easy as pie:
swapoff /home/swapfile rm /home/swapfile
If you want to make this permanent (so it’ll survive a reboot), add a line like the following to your fstab:
/home/swapfile none swap defaults 0 0
This is just a really quick tip, more details on this process can be found in the excellent Arch Wiki.