Raspberry Pi Compilation Freezing Issue#
When compiling programs on a Raspberry Pi, I encountered a problem where the Pi would freeze when compiling a large program, specifically when it reached a certain file. After shutting down and restarting, I could continue to use the Pi, but the freezing issue would occur again when compiling to the same location.
The main cause of this problem was that the default swap space set 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 blog post by a previous user, and I am very grateful for their contribution.
Step 1: Ensure that there is enough space in the system for swap space. I prepared a separate file system and added a 2GB swap file in /opt/image.
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 quick, but I had to wait for a while and started doubting my life. I thought the Pi had frozen again, restarted twice, and finally patiently waited for a few minutes. It succeeded, and tears almost came out.
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 make the newly added 2GB swap space automatically active after system restart.
sudo vim /etc/fstab
Add the following at the end of the file:
/opt/image/swap /swap swap defaults 0 0
After restarting, compile again. YES, it's successful!