Ask Your Question
0

Android Memory leak on camera rotation

asked 2015-11-27 00:57:57 -0600

Bob Woodley gravatar image

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().

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2015-11-27 01:08:08 -0600

berak gravatar image

updated 2015-11-27 02:39:32 -0600

java's gc thinks, a Mat is fairly small, like 30 bytes, it does not see the c++ allocated memory, so it will take ages, until the gc is triggered.

you'll have to (manually) release the tempMat, obtained from the transpose, at the end of your function.

edit flag offensive delete link more

Comments

Thanks for the explanation and the solution.

Bob Woodley gravatar imageBob Woodley ( 2015-11-27 10:51:36 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-11-27 00:57:57 -0600

Seen: 907 times

Last updated: Nov 27 '15