You are shown a very basic OpenCV application that simply loads bitmap images and performs linear blending on two images at a time to generate a third image. Since bitmap images (.bmp files) consumes lots of memory, you will see that eventually the application crashes. You are shown this demo and a fundamental problem that most developers would face, how do you manage memory to handle all of the images to process in a directory?
- View Offline
- [Instructor] If you have access to the exercise files folder, find the folder for this video and double-click on the BlendBitmaps folder to open it. Let's go over the contents of this folder. The first folder, bin, is where the debug and release binaries for the application are located, for both Win32 and x64. The folder called Bitmaps is where 100 picture reside, and have the .bmp extension. Bitmap image types are used here since they are not compressed, and consume much more memory. There are only 100 images in this folder so that the download size is not huge.
The application cycles through these 100 bitmaps continuously according to a reset variable that we will see in the source code. The next folder, called OpenCV_Dependencies is very important. The application will not run unless your system environment variable, called path, is set properly to point to the libraries that are in this folder. For now, just know that the Visual Studio project properties points to folders in OpenCV_Dependencies, and allows us to use OpenCV headers and libs. The last folder is where the source code is located.
Let's double-click on src. Inside the src folder, there is a readme.txt file, which contains simple step by step instructions on what to do with the OpenCV_Dependencies folder. Now, let's move up a directory and double-click on the bin folder. Then double-click Win32, then Release, and once inside here, let's right-click on BlendBitmaps.exe, and select Create Shortcut. Let's drag and drop the newly created shortcut over to our desktop for easier access.
From now on, any changes we make in the source code will be reflected on this desktop shortcut, so we don't need to locate the bin folder again. Keep in mind, this is only for the release configuration in Visual Studio. Now, on the desktop, let's double-click on the BlendBitmaps executable shortcut. This will launch three windows. Window one is a single picture of a train station. This is the first picture in the Bitmaps folder labeled pic_1.bmp. Window two is this train station being blended with multiple landscape images.
You will see the train in the background as the images are cycled through. Window three is the command prompt showing the bitmap currently being read. You'll see output reading pic_78, reading pic_80. These three windows will run for a while until a program error crash window pops up saying BlendBitmaps.exe has stopped working. Debug, or close program? Now, on this machine, it took around 60 seconds for this dialog to pop up, indicating an unexpected crash.
This time may be different on your machine. Now, before we press anything on this popup box, let's go to the command prompt window. Here we can see the OpenCV Error, insufficient memory, failed to allocate bytes in cv: outOfMemoryError, and then we can see the file and line where it failed, which is in OpenCV core module. OpenCV, Modules, Core. Basically what this is saying is we tried to load an image into memory, and because there wasn't enough memory to store the image, our application crashed.
This is the memory leak, and as you can see, it's a pretty big problem, especially since memory is different on different machines. We will see firsthand how this application manages memory using the Windows Performance Recorder and the Windows Performance Analyzer. For now, let's click on the Close program to close out of this.
- Setting up the application
- Looking at the source code
- Gathering memory leak evidence
- Optimizing the application