Raspberry Pi Compilation Freezing Issue#
When compiling programs using a Raspberry Pi, I encountered a problem where the Pi would freeze when compiling a large program and reach a certain file. After shutting down and restarting, I could continue to use the Pi, but the same freezing issue would occur when compiling to the same location.
The main cause of this problem was that the default swap space provided by the Ubuntu Mate 16.04 system installed on the Raspberry Pi was not sufficient. Compiling certain files requires a larger swap space, and since the Raspberry Pi's swap space was full, it appeared as if the Pi had frozen.
The solution is to expand the swap space in Ubuntu.
The method used here refers to a previous blog post, for which I am deeply grateful.
Step 1: Ensure that there is enough space in the system for swap space. I prepared to add a swap file in a separate file system in /opt/image, with a size of 2GB.
sudo mkdir image
sudo touch swap
Step 2: Add the swap file and set its size to 2GB using the following command:
sudo dd if=/dev/zero of=/opt/image/swap bs=1024 count=2048000
After a while, the following result will be returned:
2048000+0 records in
2048000+0 records out
2097152000 bytes (2.1 GB, 2.0 GiB) copied, 242.095 s, 8.7 MB/s
PS: This step seems to be fast, but it made me doubt life. I thought the Pi had frozen again and restarted twice. Finally, I patiently waited for a few minutes and succeeded. Tears almost fell.
Step 3: Create (set up) the swap space using the mkswap command.
sudo mkswap /opt/image/swap
Setting up swapspace version 1, size = 2 GiB (2097147904 bytes)
Step 4: Check the size of the existing swap space using the free command.
free -m
total used free shared buff/cache available
Mem: 925 185 28 14 711 660
Swap: 0 0 0
Or check the meminfo file.
grep SwapTotal /proc/meminfo
Step 5: Activate the newly added 2GB swap space using the swapon command.
sudo swapon /opt/image/swap
Step 6: Confirm that the newly added 2GB swap space is active using the free command.
free -m
total used free shared buff/cache available
Mem: 925 328 56 32 541 502
Swap: 1999 0 1999
Or check the meminfo file.
grep SwapTotal /proc/meminfo
Step 7: Modify the /etc/fstab file to automatically activate the newly added 2GB swap space after system restart.
sudo vim /etc/fstab
Add the following line at the end of the file:
/opt/image/swap /swap swap defaults 0 0
After restarting, compile again. YES, success!