pedroSG94 / RootEncoder

RootEncoder for Android (rtmp-rtsp-stream-client-java) is a stream encoder to push video/audio to media servers using protocols RTMP, RTSP, SRT and UDP with all code written in Java/Kotlin
Apache License 2.0
2.53k stars 772 forks source link

RtmpCamera2 seems to have a critical memory leak issue. #1384

Open JokerIce opened 8 months ago

JokerIce commented 8 months ago
In your demo, both the SurfaceModeRtmpActivity and TextureModeRtmpActivity use RtmpCamera2. Additionally, I have attempted to use RtmpCamera2 in both RtmpActivity and OpenGLRtmpActivity. Upon entering the Activity, without recording any video or live streaming, and then directly exiting the Activity, a memory leak error is triggered.
By adding debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.10' to your project, you can observe the log details of the memory leak issue.

==================================== HEAP ANALYSIS RESULT

                                                                                                1 APPLICATION LEAKS

                                                                                                References underlined with "~~~" are likely causes.
                                                                                                Learn more at https://squ.re/leaks.

                                                                                                129007 bytes retained by leaking objects
                                                                                                Signature: e4e8f6bdef6a4ff99124d3675fd6288d0680281b
                                                                                                ┬───
                                                                                                │ GC Root: System class
                                                                                                │
                                                                                                ├─ android.hardware.devicestate.DeviceStateManagerGlobal class
                                                                                                │    Leaking: NO (a class is never leaking)
                                                                                                │    ↓ static DeviceStateManagerGlobal.sInstance
                                                                                                │                                      ~~~~~~~~~
                                                                                                ├─ android.hardware.devicestate.DeviceStateManagerGlobal instance
                                                                                                │    Leaking: UNKNOWN
                                                                                                │    Retaining 299.2 kB in 5002 objects
                                                                                                │    ↓ DeviceStateManagerGlobal.mCallbacks
                                                                                                │                               ~~~~~~~~~~
                                                                                                ├─ java.util.ArrayList instance
                                                                                                │    Leaking: UNKNOWN
                                                                                                │    Retaining 298.1 kB in 4995 objects
                                                                                                │    ↓ ArrayList[1]
                                                                                                │               ~~~
                                                                                                ├─ android.hardware.devicestate.DeviceStateManagerGlobal$DeviceStateCallbackWrapper instance
                                                                                                │    Leaking: UNKNOWN
                                                                                                │    Retaining 129.3 kB in 2399 objects
                                                                                                │    ↓ DeviceStateManagerGlobal$DeviceStateCallbackWrapper.mDeviceStateCallback
                                                                                                │                                                          ~~~~~~~~~~~~~~~~~~~~
                                                                                                ├─ android.hardware.camera2.CameraManager$FoldStateListener instance
                                                                                                │    Leaking: UNKNOWN
                                                                                                │    Retaining 129.3 kB in 2396 objects
                                                                                                │    ↓ CameraManager$FoldStateListener.this$0
                                                                                                │                                      ~~~~~~
                                                                                                ├─ android.hardware.camera2.CameraManager instance
                                                                                                │    Leaking: UNKNOWN
                                                                                                │    Retaining 129.2 kB in 2394 objects
                                                                                                │    mContext instance of android.app.ContextImpl
                                                                                                │    ↓ CameraManager.mContext
                                                                                                │                    ~~~~~~~~
                                                                                                ├─ android.app.ContextImpl instance
                                                                                                │    Leaking: YES (ContextImpl.mOuterContext is an instance of com.pedro.streamer.texturemodeexample.
                                                                                                │    TextureModeRtmpActivity with Activity.mDestroyed true)
                                                                                                │    Retaining 129.0 kB in 2385 objects
                                                                                                │    mOuterContext instance of com.pedro.streamer.texturemodeexample.TextureModeRtmpActivity with mDestroyed = true
                                                                                                │    ↓ ContextImpl.mOuterContext
                                                                                                ╰→ com.pedro.streamer.texturemodeexample.TextureModeRtmpActivity instance
                                                                                                ​     Leaking: YES (ObjectWatcher was watching this because com.pedro.streamer.texturemodeexample.
                                                                                                ​     TextureModeRtmpActivity received Activity#onDestroy() callback and Activity#mDestroyed is true)
                                                                                                ​     Retaining 120.1 kB in 2261 objects
                                                                                                ​     key = 1236661e-c83f-45b7-b2c6-d50cd4a8c857
                                                                                                ​     watchDurationMillis = 9215
                                                                                                ​     retainedDurationMillis = 4214
                                                                                                ​     mApplication instance of com.pedro.streamer.App
                                                                                                ​     mBase instance of androidx.appcompat.view.ContextThemeWrapper
                                                                                                ====================================
                                                                                                0 LIBRARY LEAKS

                                                                                                A Library Leak is a leak caused by a known bug in 3rd party code that you do not have control over.
                                                                                                See https://square.github.io/leakcanary/fundamentals-how-leakcanary-works/#4-categorizing-leaks
                                                                                                ====================================
                                                                                                0 UNREACHABLE OBJECTS

                                                                                                An unreachable object is still in memory but LeakCanary could not find a strong reference path
                                                                                                from GC roots.
                                                                                                ====================================
                                                                                                METADATA

                                                                                                Please include this in bug reports and Stack Overflow questions.

                                                                                                Build.VERSION.SDK_INT: 33
                                                                                                Build.MANUFACTURER: HONOR
                                                                                                LeakCanary version: 2.10
                                                                                                App process name: com.pedro.streamer
                                                                                                Class count: 25429
                                                                                                Instance count: 194309
                                                                                                Primitive array count: 140083
                                                                                                Object array count: 24172
                                                                                                Thread count: 26
                                                                                                Heap total bytes: 26790552
                                                                                                Bitmap count: 5
                                                                                                Bitmap total bytes: 220689
                                                                                                Large bitmap count: 0
                                                                                                Large bitmap total bytes: 0
                                                                                                Stats: LruCache[maxSize=3000,hits=115400,misses=183421,hitRate=38%]
                                                                                                RandomAccess[bytes=9483492,reads=183421,travel=94305186323,range=32358887,size=40298187]
                                                                                                Heap dump reason: user request
                                                                                                Analysis duration: 5339 ms
                                                                                                Heap dump file path: /storage/emulated/0/Download/leakcanary-com.pedro.streamer/2024-01-16_16-05-54_504.hprof
                                                                                                Heap dump timestamp: 1705392362805
                                                                                                Heap dump duration: 1842 ms
pedroSG94 commented 8 months ago

Hello,

Thank you for the report. I will check it