Android native memory leak related to MediaCodec

116 Views Asked by At

Android Studio's memory profiler shows the native memory increases by about 50MB/hr when a video streaming app is under heavy load (12 concurrent video streams). The following is native allocations for 32 minutes: enter image description here

Here is my understanding of the call stack: _ZN7android13GraphicBuffer16addDeathCallbackENSt3_ 18functionlFvPvmEEES3 caused 151 allocations (about 5 times every minute) but only 8 deallocations, so the indicated memory leak is about 168MB.

I don't think the leak is that big. I am not highly confident on my above understanding.

The app uses MediaCodec to decode H.264 video. The call stack shows the following:

_ZN7android 10MediaCodec170nMessageReceivedERKNS_2splNS_8AMessageEEE 

The app does not have a callback function to receive messages from MediaCode. The app gets decoded data in the output buffer from MediaCodec and uses MediaCodec.releaseOutputBuffer to render it on a SurfaceView. Could anyone shed some light on the possible causes of the native memory leak? Any hint to point me in the right direction will be appreciated.

Update [2023-09-24]:

I have been doing all kinds of testing. If I change

mediaCodec.releaseOutputBuffer(iOutputBufferIndex, true);

to:

mediaCodec.releaseOutputBuffer(iOutputBufferIndex, false);

the native memory stops increasing. In other words, if the app just releases the output buffer from MediaCodec without rendering it on a SurfaceView, the issue is gone. I am not sure if MediaCodec.releaseOutputBuffer leaks memory directly or it just causes some other process to leak memory.

0

There are 0 best solutions below