Question

I've written a very very simple C++ program to read 600 video frames in a multi-threaded code. the problem is that when I release each of these frames the free memory size doesn't change! I'm using qt 4.8 and ubuntu 12.04 My Laptop Memory size is 8 GB so I don't have any memory shortage.

each frame is 1.8 MB so the total size is 600*1.8 MB = 1080 MB

before running the program my memory free size is 6.8 GB but after running my code and letting it to finish I see the free memory size is 5.9 GB so I see there's a huge memory leak in the program. I've run my code 1000 times and never saw my program crashes or face any problems so the code is okay.

My code:

#include "im_loader.h"

IM_Loader::IM_Loader(QObject *parent) :
    QThread(parent)
{
}


void IM_Loader::run()
{
    QString PATH = "/home/parsa/QtProjects/MonoSD/";
     {
     for(float i = 0 ; i < 1 ; i++)
       {
         QString Folder_Address = PATH + QString::number((int)i)+"/";
         QDir Mydir(Folder_Address);
         Mydir.setFilter(QDir::Files);
         QFileInfoList Vehicles_list = Mydir.entryInfoList();
         qDebug()<<"Address is: "<<Folder_Address<<"\n";
         qDebug()<<"Number of images are: "<<Vehicles_list.size()<<"\n";
         static int overall_counter = 0; //12451
         for(int j = 0 ; j < 600 ; j++)
           {
             if(!Stop_Disp)
             {
                QString address = Folder_Address + QString::number(overall_counter++) +".jpg";
                cv::Mat image = cv::imread(address.toUtf8().constData(),0);
                if(!image.data)
                {
                    qDebug()<<"Image Data is failed ... \n";
                    continue;
                }
                if(j%100==0)
                qDebug()<<"Folder "<<i<<" Image No. "<<j<<" is processed, Overall counter is: "<<overall_counter<<"\n";
                cv::Mat Dataaa;
                image.copyTo(Dataaa);
                QMutexLocker Locker(&Global_Mutex);
                PD_Classifier_VEC.push_back(Dataaa);
                Locker.unlock();
                }
            }
        }
     }
    qDebug("Im loader is exited now ...");
}

im_loader.h

#ifndef IM_LOADER_H
#define IM_LOADER_H


#include "Definitions.h"

class IM_Loader : public QThread
{
    Q_OBJECT
public:
    explicit IM_Loader(QObject *parent = 0);
    void run();
    bool Stop_Disp;
signals:

public slots:

};

#endif // IM_LOADER_H

Mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
QTimer *Mytimer;
QMutex Global_Mutex;
IM_Loader IML;
std::vector<cv::Mat> PD_Classifier_VEC;
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    IML.start();
    Mytimer = new QTimer(this);
    Mytimer->singleShot(20000,this,SLOT(Clear_Vec()));
}

void MainWindow::Clear_Vec()
{
    qDebug()<<"PD_Classifier_VEC.SIZE IS: "<<PD_Classifier_VEC.size();
    for(int i = 0 ; i < PD_Classifier_VEC.size() ; i++)
    {
        QMutexLocker Locker(&Global_Mutex);
        PD_Classifier_VEC.erase(PD_Classifier_VEC.begin() + i);
        Locker.unlock();
        i--;
    }
   Mytimer->singleShot(10000,this,SLOT(Clear_Vec()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

Mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include "Definitions.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    void Delay(int Milisecond_Delay);
public slots:
    void Clear_Vec();
private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

Definitions.h

#ifndef DEFINITIONS_H
#define DEFINITIONS_H
#include <QMainWindow>
#include <QThread>
#include <QTimer>
#include <QDebug>
#include <QDir>
#include <QMutex>
#include <QMutexLocker>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "im_loader.h"

extern std::vector<cv::Mat> PD_Classifier_VEC;
extern QMutex Global_Mutex;
#endif // DEFINITIONS_H

I've read in some articles that opencv c++ version support memory management so we don't have to be worry about memory leaks.

am I missing something here ? I've read these questions too but none of them worked for me this and this

Just one post amused me is SO and made me think if I have this problem in my code but I couldn't figure out. this is the link

I'll provide anything else if needed.

EDITED:

it's very interesting that if I add a Delay function in my imageloader class just after reading each frame the amount of memory leaks reduces !!!

My new im_loader.cpp

void IM_Loader::Delay(int Milisecond_Delay)
{
    double OneSecond = cv::getTickFrequency();
    int Milisecond = OneSecond/4000;

   for(double t1= 0 ; t1 < Milisecond_Delay ; t1++)
          for(double t2= 0 ; t2 < Milisecond; t2++);
}
void IM_Loader::run()
{
    QString PATH = "/home/parsa/QtProjects/FINAL_VLPR/LowSpeed/";//Dataset_PATH;
     //while(1)
     {
     for(float i = 0 ; i < 1 ; i++)
       {
         QString Folder_Address = PATH + QString::number((int)i)+"/";
         QDir Mydir(Folder_Address);
         Mydir.setFilter(QDir::Files);
         QFileInfoList Vehicles_list = Mydir.entryInfoList();
         qDebug()<<"Address is: "<<Folder_Address<<"\n";
         qDebug()<<"Number of images are: "<<Vehicles_list.size()<<"\n";
         static int overall_counter = 0; //12451
         for(int j = 0 ; j < 300 ; j++)
           {
             if(!Stop_Disp)
             {
                QString address = Folder_Address + QString::number(overall_counter++) +".jpg";
                cv::Mat image = cv::imread(address.toUtf8().constData(),0);
                Delay(100);
                if(!image.data)
                {
                    qDebug()<<"Image Data is failed ... \n";
                    continue;
                }
                if(j%100==0)
                qDebug()<<"Folder "<<i<<" Image No. "<<j<<" is processed, Overall counter is: "<<overall_counter<<"\n";
                cv::Mat Dataaa;
                image.copyTo(Dataaa);
                QMutexLocker Locker(&Global_Mutex);
                PD_Classifier_VEC.push_back(Dataaa);
                Locker.unlock();
                }
            }
        }
     }
    qDebug("Im loader is exited now ...");
}

adding a 100 ms delay reduced the memory leakage to only 100 Mega Bytes instead of 1 Gigabyte !!!

Was it helpful?

Solution

Finally, I found out what caused this memory leak ... as you can see in the im_loader class and specifically run() function I have this block of code:

 cv::Mat Dataaa;
 image.copyTo(Dataaa);
 QMutexLocker Locker(&Global_Mutex);
 PD_Classifier_VEC.push_back(Dataaa);
 Locker.unlock();

Since OpenCV use a reference counting procedure for cv::Mat object, input image is copied into Dataa variable and then into PD_Classifier. PD_Classifier gets deleted but Dataa no ! so when I removed this line and change the code to this the problem got solved:

  QMutexLocker Locker(&Global_Mutex);
  PD_Classifier_VEC.push_back(image);
  Locker.unlock();

I thought that since Dataa is a local variable it will be deleted after exiting the run() function but it appears that although it gets deleted after exiting the function its data stays on memory.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top