Status Update
Comments
ub...@gmail.com <ub...@gmail.com> #2
sa...@google.com <sa...@google.com> #3
Logs after follow mentioned steps in
===============================================
V/TAG: ViewModelFirst Created
2) Can you please provide the below requested information to better understand the issue:
Android build
Which Android build are you using? (e.g. KVT49L)
Device used
Which device did you use to reproduce this issue?
Please provide sample project or apk to reproduce the issue. Also mention the steps to be followed for reproducing the issue with the given sample project or apk.
aq...@google.com <aq...@google.com> #5
I have this problem on all my devices. Here are some of them:
1. Samsung Galaxy S9 (SM-G960F)
Android: 8.0.0 build: R16NW.G960FXXU1BRE5
2. Pixel2
Android: 9 build: PPP3.180510.008
3. Galaxy A3 (SM-A320FL)
Android: 7.0 build: NRD90M.A320FLXXS2BRAA
The three attachments included are: The built apk, the source code and a video displaying the issue.
The video showcases the app running on Galaxy S9 (Phone 1) with the following order:
1. Set "Don't keep activities" to enabled.
2. Happy case when everything is correct and onCleared() is called.
3. Fault case when the home button is pushed and onCleared() __is not called__.
aq...@google.com <aq...@google.com> #6
aq...@google.com <aq...@google.com> #7
ub...@gmail.com <ub...@gmail.com> #8
ub...@gmail.com <ub...@gmail.com> #9
ub...@gmail.com <ub...@gmail.com> #10
ub...@gmail.com <ub...@gmail.com> #11
?
Can you reproduce it there? If you can, it shouldn't be closed. If you can't maybe it should be closed too.
ub...@gmail.com <ub...@gmail.com> #12
ub...@gmail.com <ub...@gmail.com> #13
I think the issue can be closed, I am no longer able to reproduce the delayed finalization issue, and I am hoping to switch to media3 MediaSessionService anyway. I have not tried the minimal repro again, though. Ultimately I think the API 33 fixes took care of what was broken here, in addition to avoiding the referenced AndroidX leak.
Thank you for your effort to reopen this.
ub...@gmail.com <ub...@gmail.com> #14
PY Ricau, the guy behind LeakCanary, suggested that there is more to this, and that there are several actual leaks here. I will update this issue with more info as soon as I find some time, I'm in a bit of a crunch right now.
ub...@gmail.com <ub...@gmail.com> #15
The 3 LeakCanary leak reports that I attached above point at 2 distinct leaks that are still real, according to PY's analysis from the Github issue here:
Can these leaks be addressed, please?
MediaBrowserServiceCompat$ServiceHandler is an inner class of MediaBrowserServiceCompat (super class of CW) and it's there so that your service can respond to IPC callbacks. It extends Handler and is passed to a Messenger instance created by MediaBrowserServiceCompat in onCreate(), and that Messenger essentially pulls a Binder out of the Handler and passes that binder back to anything calling your service. In other words, every time something binds to your service, the thing that binds to your service is getting an indirect reference to the ServiceHandler which allows it to post messages to that handler which the service will eventually process. However, for this to work, the native framework code keeps a strong native reference locally to the binder until the calling service finalizes the ownership of the binder on its side.
This is actually a common leak pattern in the Android framework: binders are held until the other side runs garbage collection and finalizes the binder on its end, no matter what we're doing with service lifecycle (that's because binders are used beyond services, the only way to be certain the other side won't call is when it doesn't have that pointer). The proper way to handler this is for the service code to finish its handler in onDestroy, or set the reference from the handler to the service to null in onDestroy.
The first and second leak traces are identical, the problem is that MediaBrowserServiceCompat$ServiceHandler has several direct and indirect references to the service.
The last leak is the exact same issue elsewhere, ExtraSession is a stub that won't be GCed until the other side has run its own GC
https://github.com/aosp-mirror/platform_frameworks_support/blob/a9ac247af2afd4115c3eb6d16c05bc92737d6305/media/src/main/java/android/support/v4/media/session/MediaSessionCompat.java#L3413
So these 2 leaks are binder related leaks, and the leaks stay in place until a GC runs in the calling process, and you have no control over that. They really should be fixed.
aq...@google.com <aq...@google.com> #16
I've not been able to reproduce the leak on a device running Android 14 (UDC / API 34).
For unimportant reasons I had to build my own toy app from scratch (using
aq...@google.com <aq...@google.com> #17
In conclusion, I was able to reproduce on S (Android 12), but unable to reproduce the issue on T (Android 13) or U (Android 14). I'm attaching the project I used for testing in case you find anything wrong.
Can you use that project to reproduce the issue? If yes, we need to figure out what we are doing differently. Also please kindly include a bugreport (running adb bugreport
shortly after the leak is detected) if you are able to repro.
Please assign back to me once you've replied (if you are not able to, ignore).
ub...@gmail.com <ub...@gmail.com> #18
I've beefed up your example to make it repro the leaks that I attached in #12. I'll attach the updated example. The relevant changes were:
- Run service & receiver in separate process from Activity.
- Customize LeakCanary to support multiple processes.
- Add some additional media-related code to trigger a leak.
Even when not running the service in a separate process there is a leak in the Activity. I'll attach that one, too. May be a similar root cause. I have not investigated this one.
I am attaching the bugreport, too. I took it right after LeakCanary GC'ed and detected a leak. (About 5 seconds after onDestroy().) But it looks easy to reproduce now.
ub...@gmail.com <ub...@gmail.com> #19
(I did the repro on API 33)
ub...@gmail.com <ub...@gmail.com> #20
Also, I don't think non-Googlers are able to reassign an issue, so please take it from here.
aq...@google.com <aq...@google.com> #21
Thanks. Will provide an update in the next few days.
aq...@google.com <aq...@google.com> #22
Correct me if I'm wrong but the only issue we are aware that still exists is in MediaBrowserServiceCompat, right? (not in the platform MediaBrowserService)
I've sent
Once merged, you should just need to update your androidx.media dependency to pick up the fix.
ub...@gmail.com <ub...@gmail.com> #23
Thanks for the fix.
I'd think that MediaSessionCompat needs a similar fix, based on the leak traces (MediaBrowserServiceCompatLeak4.txt & MediaBrowserActivityLeak.txt) and PY's comments. You can repro MediaBrowserActivityLeak.txt by running the service in the same process as the Activity.
I'll ping PY about possibly chiming in on the code review.
aq...@google.com <aq...@google.com> #24
The change has now been merged. I'll now look into the media session compat issue, which seems to persist.
py...@squareup.com <py...@squareup.com> #25
Sorry for the late review. Not sure if you get notifications there, I left a comment:
TL;DR is that using weak refs isn't ideal, you could stick to a strong ref set to null from Service.onDestroy()
aq...@google.com <aq...@google.com> #26
Ack. Will implement the proposed fix.
ko...@gmail.com <ko...@gmail.com> #27
Есть ли утечка информации?
aq...@google.com <aq...@google.com> #28
Есть ли утечка информации?
According to translate: Is there any information leak?
No. This is a resources leak, no security issues here.
aq...@google.com <aq...@google.com> #29
Another fix for MediaSessionCompat...ExtraSession here:
Only one pending fix after this change is MediaSessionCompat...MediaSessionStub, after which I plan to mark this as fixed. Still, people should migrate to media3, but at least you can upgrade your MediaSessionCompat dep to get a quick fix of the leaks. Feel free to leave any comments here or in the patch.
aq...@google.com <aq...@google.com> #30
If there are more leaks to fix in androidx, please kindly file a fresh issue.
na...@google.com <na...@google.com> #31
The following release(s) address this bug.It is possible this bug has only been partially addressed:
androidx.media:media:1.7.0-beta01
ke...@gmail.com <ke...@gmail.com> #32
Hi im wondering if the leak happening in this issue on media3's issue tracker
is related to this.
By taking UAMP and having the MusicServiceConnection
be a Dagger @Singleton
, the MusicService
is leaked with a very similar stack trace that is exposed here, which contains only stacktraces from androidx.media
(until the MusicService
, that is):
====================================
HEAP ANALYSIS RESULT
====================================
1 APPLICATION LEAKS
References underlined with "~~~" are likely causes.
Learn more at https://squ.re/leaks.
43381 bytes retained by leaking objects
Signature: 80f093d135adc2b278b02ca2f66fbd46aaa36bea
┬───
│ GC Root: Global variable in native code
│
├─ android.support.v4.media.session.MediaSessionCompat$MediaSessionImplApi21$ExtraSession instance
│ Leaking: UNKNOWN
│ Retaining 6,7 MB in 1826 objects
│ ↓ MediaSessionCompat$MediaSessionImplApi21$ExtraSession.this$0
│ ~~~~~~
├─ android.support.v4.media.session.MediaSessionCompat$MediaSessionImplApi29 instance
│ Leaking: UNKNOWN
│ Retaining 6,7 MB in 1825 objects
│ ↓ MediaSessionCompat$MediaSessionImplApi21.mSessionFwk
│ ~~~~~~~~~~~
├─ android.media.session.MediaSession instance
│ Leaking: UNKNOWN
│ Retaining 1,8 kB in 12 objects
│ mContext instance of com.example.android.uamp.media.MusicService
│ ↓ MediaSession.mContext
│ ~~~~~~~~
╰→ com.example.android.uamp.media.MusicService instance
Leaking: YES (ObjectWatcher was watching this because com.example.android.uamp.media.MusicService received
Service#onDestroy() callback and Service not held by ActivityThread)
Retaining 43,4 kB in 896 objects
key = 11c50fab-5b0c-4267-8c51-ad235a51d861
watchDurationMillis = 5391
retainedDurationMillis = 390
mApplication instance of com.example.android.uamp.UampApplication
mBase instance of android.app.ContextImpl
====================================
`
I really dont see how using @Singleton
is any different from the default:
companion object {
// For Singleton instantiation.
@Volatile
private var instance: MusicServiceConnection? = null
fun getInstance(context: Context, serviceComponent: ComponentName) =
instance ?: synchronized(this) {
instance ?: MusicServiceConnection(context, serviceComponent)
.also { instance = it }
}
}
The issue can be reproduced on this branch of my fork of UAMP:
Which uses Dagger Hilt and removes Cast for simplification.
To reproduce:
- Open UAMP
- Play something
- Swipe the app away in the task manager
- Music Service is leaked.
From this point restarting UAMP also reveals the issue because nothing is loaded as there will be two MusicService instances.
ke...@gmail.com <ke...@gmail.com> #33
Also note that the example uses media 1.7.0 so fixes above should be included already.
aq...@google.com <aq...@google.com> #34
@ke...@gmail.com might be an issue with MediaSessionCompat, but we'll need a fresh ticket to look into this.
ke...@gmail.com <ke...@gmail.com> #35
Should I create that one then?
Description
The essential output from LeakCanary for API level 19:
* GC ROOT android.os.Handler$MessengerImpl.this$0
* references android.support.v4.media.MediaBrowserServiceCompat$ServiceHandler.this$0
* leaks com.bubenheimer.mbscl.BrowserService instance
For API level 25 (21-24 are equivalent):
* GC ROOT android.service.media.MediaBrowserService$ServiceBinder.this$0
* references android.support.v4.media.MediaBrowserServiceCompatApi24$MediaBrowserServiceAdaptor.mBase
* leaks com.bubenheimer.mbscl.BrowserService instance
com.bubenheimer.mbscl.BrowserService extends MediaBrowserServiceCompat.
My minimal app is at:
My fork of UniversalMediaPlayer with LeakCanary added:
To reproduce the issue, simply start the app, then hit the back button to close the Activity. The MediaSession gets closed at that point, and the Service is leaked.
The leak occurs also when using plain MediaBrowserService, not just the Compat variant.
I have seen MediaBrowserService get leaked in different ways when it is used more seriously (actually playing music then stopping it, showing and discarding notifications, handling user input, etc.), so when addressing this issue, please also review potential leaks in more complex use cases.
I am using the latest support library version (25.2.0) in my sample app, UniversalMediaPlayer uses 23.4.0. The leak is the same.