Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory leak in javacv

I'm trying to make a program that takes a picture from a webcam, and afterwards resizes it, converts it to HSV, and makes some thresholding on it, to find a specific color. After this is done, I use the thresholded image to find contours, and print the x,y coords of the different contours. This is repeated over and over, to make the processing from the webcam realtime.

It all works quite well, except for the fact that I am using up about 100 mb of RAM every 2 second it runs.

So far I have discovered that if I use a static picture, instead of the live images from the webcam, I can minimize the memory leak significantly, although still there is memory being consumed.

Below my code is:

public class Application {
private CaptureImage ci;
private ImageUtils iu;
private CanvasFrame canvasContours;

IplImage grabbedFrame;
IplImage resizedFrame;
IplImage thresholdedFrame;
IplImage clonedImage;

public Application(){
    ci = new CaptureImage();
    iu = new ImageUtils();
    canvasContours = new CanvasFrame("contours");

}

public void frameProcessing(){

    grabbedFrame = ci.grabImage();
    //below call used for testing purposes
    //grabbedFrame = (IplImage) opencv_highgui.cvLoadImage("testingImage.jpg");
    //cloning image due to highgui guidelines.
    clonedImage = opencv_core.cvCloneImage(grabbedFrame);
    resizedFrame = iu.resizeImage(clonedImage);

    opencv_core.cvReleaseImage(clonedImage);

    thresholdedFrame = iu.thresholdImage(resizedFrame);


    IplImage contoursFrame = iu.findContours(thresholdedFrame, resizedFrame);

    canvasContours.showImage(contoursFrame);


}
}

The grabImage is just the standard frameGrabber from javacv, which looks like this:

public class CaptureImage {
private final OpenCVFrameGrabber grabber;
private IplImage img = null;


public CaptureImage(){
    // 0-default camera, 1 - next...so on
            grabber = new OpenCVFrameGrabber(0);
            try {
                grabber.start();
            } catch (Exception e) {
                System.err.print("Failed to initialize camera");
                e.printStackTrace();
            }

}

public IplImage grabImage(){

    try {

    //A grabbed image from Logitech webcam is in following resolution: 1200x800px

        img = grabber.grab();



    } catch (Exception e) {

        e.printStackTrace();
    }
    return img;
}

I appreciate any help you can give me, and if you need any more information, please just ask!

/Jesper

like image 587
Jesper Plantener Avatar asked Nov 13 '22 09:11

Jesper Plantener


1 Answers

From your heap dump, the used memory is all byte and int arrays that are referenced from native code. Looking at your code I see that you only call cvReleaseImage for the cloned image and not for the original image.

like image 79
Ingo Kegel Avatar answered Nov 15 '22 06:11

Ingo Kegel