Ask Your Question
1

Memory deallocation in android.

asked 2016-10-13 10:43:38 -0600

Morgan Jeong gravatar image

Hello. I am a newbie at OpenCV Framework. I made an android app with OpenCV. Unfortunately, My app was crashed when i used openCV.

Main reason of crash was 'Out of Memory'.

So i called 'System.gc()' on every onCameraFrame calling. Result in, i could prevent memory usage increasing.

But, i am confused. Whether i should call release() after using mat object or not. Yes, i agree. that's way might be better. but, sample source codes do not do it.

in the sample code, mat objects is released when onCameraViewStopped() is called. but also, the mat objects is changed on every onCameraFrame() without release() calling.

so, i really wonder. should i call release function in android?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-10-31 02:43:29 -0600

Morgan Jeong gravatar image

updated 2016-10-31 02:45:39 -0600

Around Jellybean version, it has been okay with just calling System.gc(). However, above versions showed me a lot of memory leak. Especially, marshmallow.

I realized that 'release()' must be called to prevent memory leak.

Ironically, if you try to release that the mat object generated automatically by onCameraFrame.

   public Mat process(CameraBridgeViewBase.CvCameraViewFrame inputFrame)
    {
        Mat rgba = inputFrame.rgba();
        Mat gray = inputFrame.gray();
        ...

Your app will be crashed on some devices. even though, there is not any other calling release() of the mat objects on java source code.

I guess. the mat objects might be deleted on native code automatically.

edit flag offensive delete link more

Comments

the problem here is, that you're allocating a new, local Mat each time process() is called.

rather avoid that, and make all Mat's class-members.

berak gravatar imageberak ( 2016-10-31 02:54:18 -0600 )edit

yes, right you are. how wonderful i can do that. my app needs new mat objects to represent dynamic state. and aslo here is talking about release mechanism.

Morgan Jeong gravatar imageMorgan Jeong ( 2016-10-31 03:12:24 -0600 )edit

and you are right , too.

you will need to release() any local Mat created by your dynamic operation.

the main problem is, that the pixels are allocated from c++, not from java, so the gc only sees the few bytes needed for the java Mat-wrapper, and won't ever be triggered on its own.

berak gravatar imageberak ( 2016-10-31 03:17:21 -0600 )edit

I thought that as you said above. However, i was confused. Because, it was okay without any calling release() on Jellybean, the sample code made me more confused and testing devices showed me different resulting.

Thank you break for helping hand.

Morgan Jeong gravatar imageMorgan Jeong ( 2016-10-31 03:37:37 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-10-13 10:42:36 -0600

Seen: 903 times

Last updated: Oct 31 '16