Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

memory leak in java CascadeClassifier

Hei everyone,

I'm facing a memory related issue when using java impl of CascadeClassifier. I'm reading frames from a video and perform a detectMultiScale on overy Mat object obtained but memory usage is increasing fast untill the memory is full and the program crashes.

I faced similar problems every time I use a Mat object, which I need to release (throug mat.release() function) if not needed anymore, otherwise the memory usage increases till crash.

Looking the CascadeClassifier class I can see that the detectMultiScale function is:

    public  void detectMultiScale(Mat image, MatOfRect objects)
{
    Mat objects_mat = objects;
    detectMultiScale_1(nativeObj, image.nativeObj, objects_mat.nativeObj);

    return;
}

which instantiates a Mat (objects_mat) object that is never released and generates a memory leak.

I tried to extends the CascadeClassifier class with my own implementation of detectMultiScale but the method detectMultiScale_1 is private and cannot be referenced.

In my opinion with this we can avoid the memory leak in this method:

    public  void detectMultiScale(Mat image, MatOfRect objects)
{
    detectMultiScale_1(nativeObj, image.nativeObj, objects.nativeObj);

    return;
}

since we don not create a new Mat every time we perform a detection.

What do you think? Is there a simpler way to do it? (probably calling System.gc would solve the problem but it's not the optimal solution)