OpenCV 4.x: How to rescue a frozen or failed cmake/make build of OpenCV due to low RAM on a RaspberryPi.

Mohamed Faizan Cassim
2 min readNov 28, 2020
Screenshot of htop with swap-memory data visible.

My inspiration for writing this article was conceived from a problem that I faced, when I tried to compile OpenCV, from source, on the Raspberry Pi 3 Model B. At about 89% of the installation, CMake would hit a nondescript error and make a sudden exit. The root of the problem was not having enough RAM; being a meager 945mb any slightly complicated build of OpenCV would fail, bearing in mind there are other OS processes that needs to be run.

For that very reason Raspbian OS comes, by default, with a memory swap tool; that can enable part of your SD card to be used as extra RAM. But if you have not chosen to use Raspbian (probably to avoid the desktop overhead), you can still install this tool and set up your own memory swap parameters.

Here is how to do it:

1. Install dphys-swapfile (sudo apt-get install dphys-swapfile).

2. Temporarily stop dphys-swapfile (sudo dphys-swapfile swapoff)

3. Open the dphys-swapfile config file (sudo nano /etc/dphys-swapfile).

4. At the CONF_SWAPSIZE=XXXX (replace the XXXX with your desired value in bytes).

5. Initialize Swap File and run dphys-swapfile (sudo dphys-swapfile setup and sudo dphys-swapfile swapon respectively).

Once you have completed all the steps above, you should have been able to set up swap memory and you will now be able to restart your cmake/make build as usual.

--

--

Mohamed Faizan Cassim

Robotics Engineer from Kolonnawa, Sri Lanka. Lived in 4 different countries and been to 6. Programing: C, C++, C# and Python (beginner at Rust).