PublisherAdView.loadAd() blocks UI thread

1,723 views
Skip to first unread message

Lisa Wray

unread,
Jul 27, 2016, 11:30:22 AM7/27/16
to Google Mobile Ads SDK Developers
Hi, 

I've recently started integrating ads in an app I work on.  We place the ad in an item in a RecyclerView.  

However, we've noticed that calling PublisherAdView.loadAd() blocks the UI thread (and therefore scrolling) for up to a second on first load.  

I'm confident we've done our integration as recommended.  The samples are super straight forward (thank you!).  However, we can't release our app with this scrolling performance.  

Is this a known issue?  Any workarounds?

Thanks!  

Vu Chau (Mobile Ads SDK Team)

unread,
Jul 27, 2016, 11:54:52 AM7/27/16
to Google Mobile Ads SDK Developers
Hi Lisa,

It might have been a performance bottleneck that we have recently fixed.  If so, the full story is that the SDK did some memory analysis at the time of the ad request.  Before, this work was done on the main thread, and the fix was to move it to a background thread.  The fix is scheduled to go into production in a future SDK release.

If you could obtain a systrace that shows the blocking method, that'd be great so we can make a confirmation.

Thanks,

Vu Chau
Mobile Ads SDK Team

Lisa Wray

unread,
Jul 27, 2016, 11:38:21 PM7/27/16
to Google Mobile Ads SDK Developers
Hi Vu, 
Thanks for the reply.

I'm far from a systrace expert, but you don't need it to see what's going on. 

I added this code to our bind() method for our AdItem in the RecyclerView.  This code is executed on the main thread.  The first ad load was the worst -- around 1.5-2s -- but following loads were still 200+ms.   

long start = System.currentTimeMillis();
adView.loadAd(adRequest);
Timber.e("Loading ad took: %dms", System.currentTimeMillis() - start);


First run: 

AdItem: Loading ad took: 1538ms
AdItem: Loading ad took: 214ms
AdItem: Loading ad took: 215ms
AdItem: Loading ad took: 205ms
AdItem: Loading ad took: 211ms
AdItem: Loading ad took: 234ms
AdItem: Loading ad took: 284ms
AdItem: Loading ad took: 315ms
AdItem: Loading ad took: 285ms

Second run:

AdItem: Loading ad took: 2047ms
AdItem: Loading ad took: 214ms
AdItem: Loading ad took: 223ms
AdItem: Loading ad took: 246ms
AdItem: Loading ad took: 205ms
AdItem: Loading ad took: 235ms
AdItem: Loading ad took: 245ms


We're using the latest everything:
Ads SDK -- 9.2.1
Gradle: 2.2.0-alpha6
Android Studio: 2.2 preview 6
These logs are from a Nexus 5X running N preview 5 and Google Play services 9.4.52

Please advise.  Thanks.

Lisa

Vu Chau (Mobile Ads SDK Team)

unread,
Jul 28, 2016, 10:57:33 AM7/28/16
to Google Mobile Ads SDK Developers
Hi Lisa,

Thanks for the details, and I understand that checking the duration reveals this latency.  But without seeing the actual blocking method, I wouldn't be able to confirm if this instance is what our team also saw and fixed.  

Using the Traceview tool, once you have followed the instructions outlined, you should be able to obtain a .trace file.  Viewing that in Android Studio, it won't make much sense until you order the columns in the Profiling pane (Fig. 9 from the tutorial).  Look for the "Excl CPU Time %" column and order that in a descending order.  You should be able to see the blocking method that comes up at the top of the list.

Let us know which method that is (via screenshot, etc.).

Vu Chau
Mobile Ads SDK Team

Lisa Wray

unread,
Jul 28, 2016, 1:26:49 PM7/28/16
to google-adm...@googlegroups.com
Ok will do!  Thanks for the instructions!!

--

---
You received this message because you are subscribed to a topic in the Google Groups "Google Mobile Ads SDK Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-admob-ads-sdk/k4IFZA_QGT4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-admob-ads...@googlegroups.com.
To post to this group, send email to google-adm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Lisa Wray

unread,
Jul 28, 2016, 5:46:20 PM7/28/16
to Google Mobile Ads SDK Developers
Hi,

I've tried over and over, but Traceview hangs indefinitely when I run it and try to perform the initial ad load.  The ad never loads, the UI freezes, and I can't end the trace.

I took a trace of a following ad load and saw this:


Is that helpful at all?

Is there anything else I can do to try to capture a trace of the initial ad load?


On Wednesday, July 27, 2016 at 11:30:22 AM UTC-4, Lisa Wray wrote:

Lisa Wray

unread,
Jul 28, 2016, 5:50:46 PM7/28/16
to Google Mobile Ads SDK Developers
to clarify -- the app hangs indefinitely.  TraceView is still responsive, except that clicking the "stop method profiling" button doesn't seem to do anything. 

Vu Chau (Mobile Ads SDK Team)

unread,
Jul 29, 2016, 10:02:37 AM7/29/16
to Google Mobile Ads SDK Developers
Hi Lisa,

Thanks for confirming! That matches our expectation.  We have scheduled for Debug.getMemoryInfo() to be moved off of the main thread.

The fix should be live with the next SDK release.  I will update the thread once I know a rough timeline.

Vu Chau
Mobile Ads SDK Team

Lisa Wray

unread,
Jul 29, 2016, 3:54:26 PM7/29/16
to Google Mobile Ads SDK Developers
Wow, ok!  Thanks for nudging me to actually use Traceview and we look forward to the update -- hopefully won't be long.

Lisa Wray

unread,
Aug 19, 2016, 6:45:58 PM8/19/16
to Google Mobile Ads SDK Developers
Hi Vu,

Is this fix in the latest update (9.4.0)?

We're still seeing this issue.  The first load is still about 300ms and it's the same method hanging the UI thread.



Thanks for any update.

Vu Chau (Mobile Ads SDK Team)

unread,
Aug 19, 2016, 7:13:13 PM8/19/16
to Google Mobile Ads SDK Developers
Hi Lisa,

Thanks for following up on this! 

The fix wasn't included in v9.4.0.  It will be included in one of the nearest future releases of the SDK.  Once live, we will document it here, so feel free to monitor the release schedule for an update!

Vu

Rafael Ruiz Muñoz

unread,
Sep 5, 2016, 8:12:20 AM9/5/16
to Google Mobile Ads SDK Developers
I felt like giving more details about this. It's happening not only on a RecyclerView, but everywhere. It seems like it's loading a heavy widget. Fortunately you can `postDelay` this, but still it's not a good way to code on Android.

Anyway, regarding the RecyclerView, this is what I posted on StackOverflow and this is the method profiling I've done. It's pretty clear where the UI Thread is getting struggled:


those in fact are 2 initialisations (for the first time, two different `AdSize`) for `AdView`.


Could you please notify by this post when this bug has been fixed?


Thank you in advance.

Best regards.

Rafael.

Joshua Lagonera (Mobile Ads SDK Team)

unread,
Sep 5, 2016, 10:58:55 AM9/5/16
to Google Mobile Ads SDK Developers
Hi Rafael,

Can we request a sample project where you can reproduce the issue? It may be helpful in investigating the cause of the issue.
As for your other question, we will be updating you when the issue has been fixed.

Regards,
Joshua Lagonera
Mobile Ads SDK Team

Rafael Ruiz Muñoz

unread,
Sep 5, 2016, 11:10:12 AM9/5/16
to google-adm...@googlegroups.com
I'll try to make a project and put it on GitHub to be able to download it.

Thanks! :)

--

---
You received this message because you are subscribed to a topic in the Google Groups "Google Mobile Ads SDK Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-admob-ads-sdk/k4IFZA_QGT4/unsubscribe.
To unsubscribe from this group and all its topics, 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.

Vu Chau (Mobile Ads SDK Team)

unread,
Sep 6, 2016, 8:37:39 AM9/6/16
to Google Mobile Ads SDK Developers
Hi Rafael,

The bug has been fixed, and it is being deployed with the next SDK release.  Feel free to check with our release notes to stay up-to-date regarding our releases.

I noticed you are sorting the method calls based on inclusive CPU time.  I'd ask if you could send over another trace that is sorted based on exclusive time instead, since inclusive time includes time spent in all the children methods as well, which could be biased.

Thanks,

Vu Chau
Mobile Ads SDK Team

On Monday, September 5, 2016 at 11:10:12 AM UTC-4, Rafael Ruiz Muñoz wrote:
I'll try to make a project and put it on GitHub to be able to download it.

Thanks! :)

Rafael Ruiz Muñoz

unread,
Sep 6, 2016, 8:56:06 AM9/6/16
to google-adm...@googlegroups.com
Thank you Vu Chau, will download asap!
I'll let you know.

Thank you again! Really really appreciate it.
Cheers!

Rafael.

To unsubscribe from this group and all its topics, send an email to google-admob-ads-sdk+unsubscrib...@googlegroups.com.

To post to this group, send email to google-admob-ads-sdk@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rafael Ruiz Muñoz

unread,
Sep 10, 2016, 12:03:27 PM9/10/16
to Google Mobile Ads SDK Developers
Hello Vu.

This is the new screenshot I've taken, sorted by Exc. CPU Time:


I also attach the .trace file. I hope this can help you.


Is there any date for the release with the fix?

Cheers!


Rafael.



El martes, 6 de septiembre de 2016, 13:56:06 (UTC+1), Rafael Ruiz Muñoz escribió:
Thank you Vu Chau, will download asap!
I'll let you know.

Thank you again! Really really appreciate it.
Cheers!

Rafael.
To unsubscribe from this group and all its topics, 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.
For more options, visit https://groups.google.com/d/optout.
ddms6694082637194890151.trace

Vu Chau (Mobile Ads SDK Team)

unread,
Sep 11, 2016, 11:32:45 AM9/11/16
to Google Mobile Ads SDK Developers
Hi Rafael,

Thanks for sending over the requested information.  That confirms what I was thinking would be the cause of the UI blockage.  

As to the fix timeline, it should be shipped with the next release of the SDK.  Currently, the new version is being globally rolled out, and we anticipate completion towards the end of September. 

Vu Chau
Mobile Ads SDK Team

imran ali

unread,
Sep 23, 2016, 8:20:15 AM9/23/16
to Google Mobile Ads SDK Developers
Hi Vu,

Is there any expected date of release? as I also face similar scroll performace issue with RecyclerView.
I assume new fix will load dfp content on background thread so that it can't block main UI thread, is it right?

Regards
imran Ali


On Sunday, 11 September 2016 21:02:45 UTC+5:30, Vu Chau (Mobile Ads SDK Team) wrote:
Hi Rafael,

Thanks for sending over the requested information.  That confirms what I was thinking would be the cause of the UI blockage.  

As to the fix timeline, it should be shipped with the next release of the SDK.  Currently, the new version is being globally rolled out, and we anticipate completion towards the end of September. 

Vu Chau
Mobile Ads SDK Team

On Saturday, September 10, 2016 at 12:03:27 PM UTC-4, Rafael Ruiz Muñoz wrote:
Hello Vu.

This is the new screenshot I've taken, sorted by Exc. CPU Time:


I also attach the .trace file. I hope this can help you.


Is there any date for the release with the fix?

Cheers!


Rafael.



El martes, 6 de septiembre de 2016, 13:56:06 (UTC+1), Rafael Ruiz Muñoz escribió:
Thank you Vu Chau, will download asap!
I'll let you know.

Thank you again! Really really appreciate it.
Cheers!

Rafael.
To post to this group, send email to google-adm...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--

---
You received this message because you are subscribed to a topic in the Google Groups "Google Mobile Ads SDK Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-admob-ads-sdk/k4IFZA_QGT4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-admob-ads-sdk+unsub...@googlegroups.com.
To post to this group, send email to google-adm...@googlegroups.com.

Veer Arjun Busani(Mobile Ads SDK Team)

unread,
Sep 23, 2016, 12:32:53 PM9/23/16
to Google Mobile Ads SDK Developers
Hi Imran,

The latest released GMS 9.6.0 is live and must include this fix. Try with this version and let us know if your issue has been resolved.

Thanks,
Arjun Busani
Mobile Ads SDK Team

Rafael Ruiz Muñoz

unread,
Sep 23, 2016, 3:40:44 PM9/23/16
to google-adm...@googlegroups.com
I works perfect now!

Thanks! 😍😍

To unsubscribe from this group and all its topics, send an email to google-admob-ads-sdk+unsubscrib...@googlegroups.com.

To post to this group, send email to google-adm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--

---
You received this message because you are subscribed to a topic in the Google Groups "Google Mobile Ads SDK Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-admob-ads-sdk/k4IFZA_QGT4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-admob-ads-sdk+unsubscrib...@googlegroups.com.

To post to this group, send email to google-adm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--

---
You received this message because you are subscribed to a topic in the Google Groups "Google Mobile Ads SDK Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-admob-ads-sdk/k4IFZA_QGT4/unsubscribe.
To unsubscribe from this group and all its topics, 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.

imran ali

unread,
Sep 25, 2016, 7:55:01 AM9/25/16
to Google Mobile Ads SDK Developers
Thank you very much Arjun,

I see approx 90% loading time has reduced on main thread, that is great help for us.
One thing I notice very first call `publisherAdView.loadAd(mPublisherAdRequest);` is taking same amount of time as pervious on main thread, next call onwards is perfect.
Can you fix this as well?


Regards
Imran Ali

imran ali

unread,
Sep 26, 2016, 1:07:19 AM9/26/16
to Google Mobile Ads SDK Developers
Hi Arjun.

I see first call issue in latest released GMS 9.6.0. So I am requesting please double check at sdk end.

```I see approx 90% loading time has reduced on main thread, that is great help for us.
One thing I notice very first call `publisherAdView.loadAd(mPublisherAdRequest);` is taking same amount of time as pervious on main thread, next call onwards is perfect.
Can you fix this as well?```

Regards
Imran Ali

Vu Chau (Mobile Ads SDK Team)

unread,
Sep 26, 2016, 11:00:04 AM9/26/16
to Google Mobile Ads SDK Developers
Hi Imran,

Glad to hear that the performance of the newest SDK release is more optimal for everyone.
One thing I notice very first call `publisherAdView.loadAd(mPublisherAdRequest);` is taking same amount of time as pervious on main thread, next call onwards is perfect.
How much longer does the first ad request take compared to the rest? Are you able to put in some logging to get some timestamps for us?

Thanks,

Vu Chau
Mobile Ads SDK Team 

Lisa Wray

unread,
Oct 24, 2016, 6:03:24 PM10/24/16
to Google Mobile Ads SDK Developers
We're also still seeing a delay on ad requests.  The first ad load (in an application session) is still the slowest.  

I just fresh launched our application several times and scrolled to an ad.  Here are the first four times (by System.currentTimeMillis): 349ms, 414ms, 1266ms, 1153ms.

Following ad loads were much quicker: 19ms, 11ms, 12ms, 23ms.  However, I'd still like to stress that none of this should be happening on the main thread at all!  It means we're guaranteed to drop multiple frames in the best possible case.

I'll try to follow up with a systrace.  

In the meantime, is there any way to force this initialization (whatever it is) to happen sometime earlier?

Vu Chau (Mobile Ads SDK Team)

unread,
Oct 25, 2016, 10:30:42 AM10/25/16
to Google Mobile Ads SDK Developers
Hi Lisa,

That's strange that there is activity from the ads package on the main thread.  When you can, please forward us the traceview and systrace results, as well as a minimal zipped project with which you saw the latencies.  Please also confirm the version of Google Play services / Firebase that is compiled in your app.

There isn't a way to force the initialization earlier than scheduled.  Given our in-house testing, there was minimal to no latency, so we did not see a need to introduce preloading.

Vu Chau
Mobile Ads SDK Team

Lisa Wray

unread,
Oct 25, 2016, 12:05:59 PM10/25/16
to Google Mobile Ads SDK Developers
Hi Vu -- I created a new thread for this as I'm not sure it's related to the original problem.  

Here's the thread:

Here is a minimal example project reproducing the problem.  Make sure to fresh launch it rather than from background and kill it completely between launches.  
Reply all
Reply to author
Forward
0 new messages