memory leak in java CascadeClassifier

asked 2016-07-15 04:49:15 -0500

vzamboni gravatar image

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);


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);


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)

edit retag flag offensive close merge delete


Wait the garbage collector will delete the objects_mat from the second you reach that return statement? So I do not see why you should not do this?

StevenPuttemans gravatar imageStevenPuttemans ( 2016-07-15 08:56:04 -0500 )edit

me neither, but memory is getting full if I execute a lot of detectMultiScale. I have to manually call System.gc to empty memory and avoid crashes, but I would like to avoid this. release the objects_mat would solve the problem.

vzamboni gravatar imagevzamboni ( 2016-07-15 09:04:16 -0500 )edit