Banner ads leak associated Activity?

545 views
Skip to first unread message

Lucas

unread,
Nov 1, 2018, 3:20:14 PM11/1/18
to Google Mobile Ads SDK Developers
The attached app is the AdMob BannerExample from https://github.com/googleads/googleads-mobile-android-examples/releases/download/5.3/Java_BannerExample_AdMob.zip that's been slightly modified to allow reporting of memory leaks via LeakCanary.

If you open the app, wait for the ad to appear and then click on Back to 'close' the app then Leak Canary will report a leak of MyActivity about 5 seconds later.  I've attached a sample heap dump from one such leak report.  This is on a Moto G5 updated to Android 8.1.0. I built the app in Android Studio 3.1.4.

If I comment out all the code in MyActivity.onCreate() after the call to setContentView() then no leak is reported.

Is this a genuine memory leak in AdMob?  I've seen similar leaks reported in my own app.

Thanks, Lucas.
Message has been deleted
Message has been deleted

Lucas

unread,
Nov 1, 2018, 3:22:35 PM11/1/18
to Google Mobile Ads SDK Developers
bannerexample.MyActivity.heapdump.txt
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted

Lucas

unread,
Nov 1, 2018, 3:27:08 PM11/1/18
to Google Mobile Ads SDK Developers
Struggling to attach the sample app. Every time I try I get: There was an error posting the message to the group. Please try again later.


On Thursday, 1 November 2018 19:20:14 UTC, Lucas wrote:
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted

Lucas

unread,
Nov 2, 2018, 11:08:19 AM11/2/18
to Google Mobile Ads SDK Developers
I still can't attach the sample app. Is there someone I can email it to in Google please?
Thanks, Lucas.

mobileadssdk-a...@google.com

unread,
Nov 2, 2018, 3:40:11 PM11/2/18
to Lucas, Google Mobile Ads SDK Developers
Hi Lucas,

Thanks for the sample app that you have attached. I have not been able to reproduce any issue such as a memory leak. However, can you confirm whether you are noticing without the GMA SDK instances in the MainActivity and let us know?

Regards,
Deepika Uragayala
Mobile Ads SDK Team
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog and Google+ page:
    http://googleadsdeveloper.blogspot.com
    https://plus.google.com/115658573333388777174/
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~

--

---
You received this message because you are subscribed to the Google Groups "Google Mobile Ads SDK Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-admob-ads-sdk+unsub...@googlegroups.com.
To post to this group, send email to google-admob-ads-sdk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-admob-ads-sdk/a61a850a-f932-46c5-834d-a4cbdc8e3d25%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Lucas

unread,
Nov 3, 2018, 6:08:12 AM11/3/18
to Google Mobile Ads SDK Developers
Hi Deepika,

Thanks for looking at this.  Sam on this thread - https://groups.google.com/d/msg/google-admob-ads-sdk/XFLxzvMBw30/PWZmvUkEAgAJ - said he'll also look at it.

I'm surprised you're not able to reproduce it.  I already said in my OP that no leaks are reported by LeakCanary if I comment out the code following setContentView() in MyActivity.onCreate().  But if I uncomment the code the leak reliably reappears when I tap on Back to exit the app. In fact, both MyActivity and ReportFragment are leaked. Heapdumps of both are attached.

Also, in my own app I get the same pathway of objects in the tree of references: b.a -> p.C -> ang.c -> a.b -> [Activity]. I sometimes get another chain of references involving GMA objects when my app's activity is leaked.

Lucas.
bannerexample_03Nov2018..zip

Francisco Soto

unread,
Nov 3, 2018, 10:07:56 AM11/3/18
to Google Mobile Ads SDK Developers
My 2 cents here:

PublisherAdView after being destroy()ed still have an instance of com.google.android.gms.ads.internal.webview which contains a retained instance of the Activity. 

So yes, it can generate a memory leak.

Chi Ho Kwok

unread,
Nov 4, 2018, 2:54:44 PM11/4/18
to Google Mobile Ads SDK Developers
Related to this issue: AdMob banners causes high idle CPU usage due to WebView leak.

When there are any alive WebViews in a process, it will use a ton of CPU trying to synchronize with vsyncs, even while the display is off.

To reproduce:
  1. Clone https://github.com/chkwok/googleads-mobile-android-examples - this updates play-services-ads to 17.0.0 to make sure bug isn't fixed already and adds some Log.i to onResume/Pause/Destroy of the activity. You can use the upstream version as well, that also has the bug, but no Log messages though.
  2. Launch the BannerExample app
  3. Press back. Verify in LogCat that BannerExample is .onDestroy-ed.
  4. adb shell top
  5. com.google.android.gms.example.bannerexample will be at the top of the CPU usage at 15-20% of a core. Forever. Until the VM is killed by the Android.
  6. This causes "<App name> is draining your battery" notifications to show if your process is long living. Which is true. If you ever showed an Admob banner, even if the activity is long gone, your process will burn 0.2 cores of CPU to warm your hands, just in time for winter.
Screenshots attached:
  1. Profiler output of a frame. It repeats this at 60 hz.
  2. Screenshot of adb shell top.

This is a huge issue, leaking a WebView is a major error that we developers cannot fix for you. So far, I tried removeView on the adView, pause, stop and destroy, but somewhere deep inside the ads SDK, there are still living WebView objects that haven't been destroy()-ed.


Please escalate this issue, I have to decide between killing users' batteries and zero AdMob revenue. This is a huge deal as it affects every single developer using AdMob and indirectly, basically every single Android user.


Note that to really fix this issue, all instantiated WebViews must be destroyed manually, or automatically when the attached Activity dies. A single line with "new WebView(this)" that doesn't do anything with the return value will permanently break the application until VM restart. All unattached WebView instances must be destroyed manually when all ads are stopped.


I posted this as a reply as Banner leaks WebView leaks Activity, and the guys above me already pinpointed the leak path. Let me know if this should be on a separate thread.


Kind regards,

Chi Ho Kwok
1-profile.png
2-top.png

Chi Ho Kwok

unread,
Nov 4, 2018, 4:06:15 PM11/4/18
to Google Mobile Ads SDK Developers
A small update:


Showing a banner is not required. Just running MobileAds.initialize() is enough to get notified for every vsync on the main thread due to a WebView leak.

Minimum repro changed to:
  1. In Activity, comment out setContentView and adView.loadAd. The other lines are harmless.
  2. No AdView is created, so there's no way we can accidentally leak one or miss some pause/stop/destroy events.
  3. Start BannerExample.
  4. Press back or home to pause / stop the app.
  5. Go to Android Studio, profiler, take a instrumented Java profile.
  6. The same profile will continue to show up, Choreographer keeps notifying the app on every vsync burning CPU for nothing.
  7. Note that the only thing this app does is call MobileAds.initialize().
It may be a good idea to escalate this to engineering ASAP and talk to the Chromium devs on how to use WebViews correctly to prevent this.


Kind regards,

Chi Ho Kwok


mobileadssdk-a...@google.com

unread,
Nov 5, 2018, 3:02:16 PM11/5/18
to Chi Ho Kwok, Google Mobile Ads SDK Developers
Hi there,

Can you try to reproduce this when you declare the AppID via the AndroidManifest and not use the initialize() call? This method is now deprecated. Let us know if you are noticing this with our sample apps.

Regards,
Deepika Uragayala
Mobile Ads SDK Team
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog and Google+ page:
    http://googleadsdeveloper.blogspot.com
    https://plus.google.com/115658573333388777174/
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~

To unsubscribe from this group and stop receiving emails from it, send an email to google-admob-ads-sdk+unsubscrib...@googlegroups.com.

--

---
You received this message because you are subscribed to the Google Groups "Google Mobile Ads SDK Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-admob-ads-sdk+unsub...@googlegroups.com.
To post to this group, send email to google-admob-ads-sdk@googlegroups.com.

Chi Ho Kwok

unread,
Nov 5, 2018, 3:21:41 PM11/5/18
to Google Mobile Ads SDK Developers

Basically comment out the MobileAds.initialize call and leave the rest in place. Tested against google-play-ads 17.0.0. You did run it yourself to verify the report right? It shouldn't be that hard to do this test yourself.


For others watching this thread: https://www.reddit.com/r/androiddev/comments/9u6z64/psa_admob_is_causing_high_idle_cpu_usage_issue/ has a workaround but it involves pausing animation callbacks with reflection in Choreographer, I wouldn't call it a fix but at least this stops the battery drain for now. Requires targetSdk < 28, yes, smaller than, as it uses secret "dark greylisted" API.

Did you hear anything from engineering yet? Billions devices are burning battery due to this bug.


Chi Ho

Carl Hasselskog

unread,
Nov 6, 2018, 1:46:28 AM11/6/18
to Google Mobile Ads SDK Developers
I can confirm that the same bug appears in our app as well. Phone: Google Pixel. OS: Android 8.0. App: com.degoo.android. Target SDK 28.

What's the ETA of this? Given Google's punishment of bad behavior via Android Vitals one would expect Google libraries to behave more appropriately. 

Lucas

unread,
Nov 6, 2018, 1:45:28 PM11/6/18
to Google Mobile Ads SDK Developers
@Deepika: Interesting. Yes, if I comment out the call to MobileAds.initialize() in the sample app I submitted originally then no memory leak is reported by Leak Canary. And if I uncomment that line then a leak is reported again.  Unfortunately Leak Canary reports 'Leak analysis failed' so I can't give more details.

I didn't know that call was deprecated. I'll try the same in my own app too and report back...

Chi Ho's CPU-burn issue may or may not be related. Deepika or someone else from Google needs to decide whether that should be discussed on a separate thread to this one regarding the memory leak.

Thanks, Lucas.


11-06 18:35:29.706 9308-9308/com.google.android.gms.example.bannerexample W/Ads: #004 The webview is destroyed. Ignoring action.

Lucas

unread,
Nov 6, 2018, 1:56:23 PM11/6/18
to Google Mobile Ads SDK Developers
@Deepika: Wow! Yes, if all I do is comment out MobileAds.initialize()  in my own app then the memory leak disappears! And if I uncomment it the leaks reappear. That's pretty conclusive.

BTW I'm using this in the module's build.gradle for both my app and the sample app I submitted:
implementation 'com.google.android.gms:play-services-ads:17.0.0'

Based on this the initialization instructions at https://developers.google.com/admob/android/quick-start#initialize_mobileads need updating asap as they're wrong at the moment.

mobileadssdk-a...@google.com

unread,
Nov 6, 2018, 3:12:07 PM11/6/18
to Carl Hasselskog, Google Mobile Ads SDK Developers
Hi All,

Can you guys confirm that this is happening when you set the AppID via the Manifest and not from the @deprecated initialize() method and then let us know? The initialize() method has been deprecated in v17.x. You can learn more about this here.

Regards,
Deepika Uragayala
Mobile Ads SDK Team
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog and Google+ page:
    http://googleadsdeveloper.blogspot.com
    https://plus.google.com/115658573333388777174/
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~

--

---
You received this message because you are subscribed to the Google Groups "Google Mobile Ads SDK Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-admob-ads-sdk+unsub...@googlegroups.com.
To post to this group, send email to google-admob-ads-sdk@googlegroups.com.

Carl Hasselskog

unread,
Nov 6, 2018, 3:25:23 PM11/6/18
to Google Mobile Ads SDK Developers
Yes, we have set com.google.android.gms.ads.APPLICATION_ID

Lucas

unread,
Nov 6, 2018, 3:35:31 PM11/6/18
to Google Mobile Ads SDK Developers
@Deepika - yes, I've set com.google.android.gms.ads.APPLICATION_ID in both my own app and the sample app. If I only do that then I get no memory leaks in either app. But if I call MobileAds.initialize() as well then I get the memory leaks.

Carl Hasselskog

unread,
Nov 6, 2018, 6:56:53 PM11/6/18
to Google Mobile Ads SDK Developers
Although we have com.google.android.gms.ads.APPLICATION_ID defined it could be that our mediation SDK calls MobileAds.initialize() anyway, so I can't guarantee that it's not called. 

mobileadssdk-a...@google.com

unread,
Nov 7, 2018, 3:54:26 PM11/7/18
to Carl Hasselskog, Google Mobile Ads SDK Developers
Hi Carl,

Then it would be an issue with the mediation SDK as this method is completely deprecated. Can you confirm the mediation SDK that might be calling this API?

Regards,
Deepika Uragayala
Mobile Ads SDK Team
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog and Google+ page:
    http://googleadsdeveloper.blogspot.com
    https://plus.google.com/115658573333388777174/
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~

To unsubscribe from this group and stop receiving emails from it, send an email to google-admob-ads-sdk+unsubscrib...@googlegroups.com.

--

---
You received this message because you are subscribed to the Google Groups "Google Mobile Ads SDK Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-admob-ads-sdk+unsub...@googlegroups.com.
To post to this group, send email to google-admob-ads-sdk@googlegroups.com.

Carl Hasselskog

unread,
Dec 15, 2018, 7:23:29 AM12/15/18
to Google Mobile Ads SDK Developers
Adincube. 

Regards
Carl

mobileadssdk-a...@google.com

unread,
Dec 17, 2018, 3:33:04 PM12/17/18
to Carl Hasselskog, Google Mobile Ads SDK Developers
Hi Carl,

We will not be able to provide any official support for third party integrations. This looks like an issue with the mediation SDK( Adincube) and I'd suggest that you post your concern on StackOverflow or Adincube's support for further assistance on this.

Regards,
Deepika Uragayala
Mobile Ads SDK Team
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog and Google+ page:
    http://googleadsdeveloper.blogspot.com
    https://plus.google.com/115658573333388777174/
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~

Adincube. 

Regards

--

---
You received this message because you are subscribed to the Google Groups "Google Mobile Ads SDK Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-admob-ads-sdk+unsub...@googlegroups.com.
To post to this group, send email to google-admob-ads-sdk@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages