Ask Your Question

Revision history [back]

Android Memory leak on camera rotation

I have an app that needs to run in portrait mode and I want to have a small camera preview displayed on the screen. Opencv defaults to landscape for preview. The following code rotates it back to portrait:

 public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
    mRgba = inputFrame.rgba();

    Mat tempMat = mRgba.t();
    Core.flip(tempMat, mRgbaT, 1);
    Imgproc.resize(mRgbaT, dst, mRgba.size());
    return dst;
}

The problem is that this code leaks memory and crashes after 10 or 15 seconds. I have tried various permutations of using release and using temporary matrices. It always seems to crash on the resize() function so I suppose that is where the leak is.

For the above example these are the declarations:

private Mat mRgba;
private Mat mRgbaT;
private Mat dst;

initialized in onCameraViewStarted():

public void onCameraViewStarted(int width, int height) {
    mRgbaT = new Mat();
    dst = new Mat();
}

I need help managing the memory correctly. I'd also like to understand what the problem is. Since Java is garbage collected it feels mighty strange to be required to call release().