2

我创建了一个实现 MediaBrowserServiceCompat 的测试应用程序。我已遵循本指南: https ://developer.android.com/guide/topics/media-apps/audio-app/building-a-mediabrowserservice 创建了 MediaPlaybackService 和 MainActivity。我在 onDestroy 方法中添加了泄漏金丝雀并添加了 AppWatcher.objectWatcher.watch(this)。在打开和退出应用程序时,leak canary 发现一个泄漏:

6153 bytes retained
    ┬
    ├─ android.service.media.MediaBrowserService$ServiceBinder
    │    Leaking: UNKNOWN
    │    GC Root: Global variable in native code
    │    ↓ MediaBrowserService$ServiceBinder.this$0
    │                                        ~~~~~~
    ├─ androidx.media.MediaBrowserServiceCompat$MediaBrowserServiceImplApi26$MediaBrowserServiceApi26
    │    Leaking: UNKNOWN
    │    MediaBrowserServiceCompat$MediaBrowserServiceImplApi26$MediaBrowserServiceApi26 does not wrap an activity context
    │    ↓ MediaBrowserServiceCompat$MediaBrowserServiceImplApi26$MediaBrowserServiceApi26.mBase
    │                                                                                      ~~~~~
    ╰→ com.example.mediabrowsertestapp.MediaPlaybackService
    ​     Leaking: YES (ObjectWatcher was watching this)
    ​     MediaPlaybackService does not wrap an activity context
    ​     key = 11f40383-1498-4743-9f20-208cbd2839a1
    ​     watchDurationMillis = 5191
    ​     retainedDurationMillis = 183

Please include this in bug reports and Stack Overflow questions.

    Build.VERSION.SDK_INT: 28
    Build.MANUFACTURER: HMD Global
    LeakCanary version: 2.0
    App process name: com.example.mediabrowsertestapp
    Analysis duration: 8967 ms
    Heap dump file path: /data/user/0/com.example.mediabrowsertestapp/files/leakcanary/2019-12-10_10-21-47_693.hprof
    Heap dump timestamp: 1575969720525

由于该应用程序仅包含来自 google 示例的代码,因此我无法弄清楚如何处理此泄漏。我应该忽略它吗?

代码: https ://github.com/finneapps/MediaBrowserService-memory-leak

package com.example.mediabrowsertestapp

import android.os.Bundle
import android.support.v4.media.MediaBrowserCompat
import android.support.v4.media.session.MediaSessionCompat
import android.support.v4.media.session.PlaybackStateCompat
import androidx.media.MediaBrowserServiceCompat
import leakcanary.AppWatcher

private const val LOG_TAG = "MediaPlaybackService"

class MediaPlaybackService : MediaBrowserServiceCompat() {

    private var mediaSession: MediaSessionCompat? = null
    private lateinit var stateBuilder: PlaybackStateCompat.Builder

    override fun onCreate() {
        super.onCreate()
        mediaSession = MediaSessionCompat(baseContext, LOG_TAG).apply {
            setFlags(
                MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS
                        or MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS
            )
            stateBuilder = PlaybackStateCompat.Builder()
                .setActions(
                    PlaybackStateCompat.ACTION_PLAY
                            or PlaybackStateCompat.ACTION_PLAY_PAUSE
                )
            setPlaybackState(stateBuilder.build())
            setSessionToken(sessionToken)
        }
    }

    override fun onGetRoot(
        clientPackageName: String, clientUid: Int,
        rootHints: Bundle?
    ): BrowserRoot? {
        return BrowserRoot(LOG_TAG, null)
    }

    override fun onLoadChildren(
        parentMediaId: String,
        result: Result<List<MediaBrowserCompat.MediaItem>>
    ) {
        result.sendResult(emptyList())
    }

    override fun onDestroy() {
        super.onDestroy()
        AppWatcher.objectWatcher.watch(this)
    }
}
package com.example.mediabrowsertestapp

import android.content.ComponentName
import android.media.AudioManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.media.MediaBrowserCompat
import android.support.v4.media.MediaMetadataCompat
import android.support.v4.media.session.MediaControllerCompat
import android.support.v4.media.session.PlaybackStateCompat

class MainActivity : AppCompatActivity() {
    private val controllerCallback = object : MediaControllerCompat.Callback() {

        override fun onMetadataChanged(metadata: MediaMetadataCompat?) {}

        override fun onPlaybackStateChanged(state: PlaybackStateCompat?) {}
    }

    private lateinit var mediaBrowser: MediaBrowserCompat

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mediaBrowser = MediaBrowserCompat(
            this,
            ComponentName(this, MediaPlaybackService::class.java),
            connectionCallbacks,
            null 
        )
    }

    override fun onStart() {
        super.onStart()
        mediaBrowser.connect()
    }

    override fun onResume() {
        super.onResume()
        volumeControlStream = AudioManager.STREAM_MUSIC
    }

    override fun onStop() {
        super.onStop()
        MediaControllerCompat.getMediaController(this)?.unregisterCallback(controllerCallback)
        mediaBrowser.disconnect()
    }

    private val connectionCallbacks = object : MediaBrowserCompat.ConnectionCallback() {
        override fun onConnected() {
            mediaBrowser.sessionToken.also { token ->
                val mediaController = MediaControllerCompat(
                    this@MainActivity, // Context
                    token
                )
                MediaControllerCompat.setMediaController(this@MainActivity, mediaController)
            }

        }

        override fun onConnectionSuspended() {

        }

        override fun onConnectionFailed() {

        }
    }
}
apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.example.mediabrowsertestapp"
        minSdkVersion 15
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation "androidx.media:media:1.1.0"
    debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
4

1 回答 1

6

您的媒体服务扩展了 MediaBrowserServiceCompat。起初,这看起来像是 MediaBrowserServiceCompat 的问题。androidx.media:media:1.1.0是最新版本,MediaBrowserServiceCompat 的最新来源目前在这里

MediaBrowserServiceCompat 是一个基础服务类,它委托给 AOSP MediaBrowserService 类 ( sources ) 的子类。这里有一个棘手的问题是,虽然 MediaBrowserService 是一项服务,但当 MediaBrowserServiceCompat 使用它时,它实际上并不是作为真正的 Android 服务创建的,而是作为 MediaBrowserServiceCompat 将回调传递给的单纯委托而创建的。这本身就意味着很容易犯错误。

MediaBrowserService 子类包含对 MediaBrowserServiceCompat 实例的引用,以便

泄漏跟踪显示存在对 MediaBrowserService$ServiceBinder 的本机引用。当 MediaBrowserServiceCompat 收到它的 onBind() 调用时,它会从 MediaBrowserService 返回绑定器。只要 MediaBrowserServiceCompat 还活着,就应该保留该绑定器,并在它被销毁时释放。此时我们需要一个堆转储来进一步挖掘。

我下载了源代码,构建了应用程序并将其部署在模拟器 (API 29) 上,并且能够通过按下返回来重现泄漏。我注意到 MediaSessionCompat 构造函数 javadoc 声明“您必须在会话完成时调用 {@link #release()}。”。我尝试在 onDestroy() 中调用它,但泄漏仍然发生。

我想知道这是否只发生在 app compat 或 AOSP 上。我将代码移植回 AOSP(无兼容),同样的事情正在发生。

┬
├─ android.service.media.MediaBrowserService$ServiceBinder
│    Leaking: UNKNOWN
│    GC Root: Global variable in native code
│    ↓ MediaBrowserService$ServiceBinder.this$0
│                                        ~~~~~~
╰→ com.example.mediabrowsertestapp.MediaPlaybackService
​     Leaking: YES (ObjectWatcher was watching this)
​     MediaPlaybackService2 does not wrap an activity context
​     key = e9c30a2e-e06e-4c4b-b375-f8c8c1482761
​     watchDurationMillis = 5214
​     retainedDurationMillis = 179

METADATA

Build.VERSION.SDK_INT: 25
Build.MANUFACTURER: Google
LeakCanary version: 2.0
App process name: com.example.mediabrowsertestapp
Analysis duration: 2159 ms

我尽可能多地删除了代码,然后看到泄漏仍在发生。这是最终的代码:

class MediaPlaybackService : MediaBrowserService() {

    override fun onLoadChildren(
        parentId: String,
        result: Result<MutableList<MediaBrowser.MediaItem>>
    ) {
        result.sendResult(mutableListOf())
    }

    override fun onGetRoot(
        clientPackageName: String, clientUid: Int,
        rootHints: Bundle?
    ): BrowserRoot? {
        return BrowserRoot("MediaPlaybackService", null)
    }


    override fun onDestroy() {
        super.onDestroy()
        AppWatcher.objectWatcher.watch(this)
    }
}
class MainActivity : Activity() {
    private lateinit var mediaBrowser: MediaBrowser

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mediaBrowser = MediaBrowser(
            this,
            ComponentName(this, MediaPlaybackService::class.java),
            connectionCallbacks,
            null
        )
    }

    override fun onStart() {
        super.onStart()
        mediaBrowser.connect()
    }

    override fun onStop() {
        super.onStop()
        mediaBrowser.disconnect()
    }

    private val connectionCallbacks = object : MediaBrowser.ConnectionCallback() {
        override fun onConnected() {
        }

        override fun onConnectionSuspended() {

        }

        override fun onConnectionFailed() {

        }
    }
}

这应该是针对最新 Android 版本提出的问题,即使它已经存在了一段时间。按照设计,进程间调用会导致活页夹在内存中的保存时间比预期的要长。当 MediaBrowserService 被销毁时,MediaBrowserService.ServiceBinder 应该释放它对其外部类 MediaBrowserService 的引用。

这是在 AOSP 中重现它的 PR:https ://github.com/finneapps/MediaBrowserService-memory-leak/pull/1

于 2019-12-10T22:32:39.903 回答