Memory leak while using admob interstitial ads

1,321 views
Skip to first unread message

Venkatramanan

unread,
Jul 23, 2014, 1:58:21 AM7/23/14
to google-adm...@googlegroups.com

I have service which will show an activity at particular point of time, after that activity on every 13th time i am showing an admob interstitial ads. My application's RAM usage is increasing by 20MB when the interstitial ad is shown and after that it is not getting garbage collected. On the next 13th time when another interstitial ads is shown there is no increase in the service memory.

My code for showing ads :

public void loadAndShowInterstitialAd() {
        interstitial = new InterstitialAd(getApplicationContext());
        interstitial.setAdUnitId(AD_UNIT_ID);

        final AdRequest adRequest = new AdRequest.Builder()
                .build();

        Handler handler = new Handler(new Handler.Callback() {

            @Override
            public boolean handleMessage(Message msg) {
                interstitial.loadAd(adRequest);
                return true;
            }
        });

        if (handler != null) {
            handler.sendEmptyMessageDelayed(0, 200);
        }

        interstitial.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                super.onAdLoaded();
                displayInterstitial();
            }

        });
    }

    public void displayInterstitial() {
        if (interstitial.isLoaded()) {
            interstitial.show();
        }
    }

I have tried few solutions in the following stack overflow questions, but nothing worked for me.

Android Admob Interstitial Memory leak

Android AdMob causes memory leak?

Please help me to fix this issue. Thanks in advance.


Regards,

Venkat.

Eric Leichtenschlag

unread,
Jul 23, 2014, 6:23:18 PM7/23/14
to google-adm...@googlegroups.com
Hi Venkat,

From your first line of code, I see your class holds onto the Interstitial object. So if you're keeping your class around, then the interstitial wouldn't get garbage collected. What happens if you null out the interstitial in the onAdClosed event of your AdListener implementation:

      @Override
      public void onAdClosed() {
        interstitial = null;
      }

Thanks,
Eric

Venkatramanan

unread,
Jul 24, 2014, 2:17:45 AM7/24/14
to google-adm...@googlegroups.com
Hi Eric,

There is no change in memory leak even after putting interstitial ad to null in the onAdClosed.
    @Override
      public void onAdClosed() {
        interstitial = null;
      }

Is there any other solution to this issue?

Thanks & Regards,
Venkat.

Eric Leichtenschlag

unread,
Jul 25, 2014, 3:21:50 PM7/25/14
to google-adm...@googlegroups.com
I don't have any other recommendations.

Can you provide more information on how you're inspecting ram usage, and what you're seeing increase? I tried:

adb shell dumpsys meminfo <your.app.package>

I ran this before and after requesting interstitials, in both cases with setting the interstitial to null and not setting it to null. Setting it to null helped for me. The total memory used after showing and nulling an interstitial was a little higher than it was before I requested any ads, but that's likely due to the SDK caching some stuff to make future requests faster.

Thanks,
Eric

Venkatramanan

unread,
Jul 26, 2014, 4:59:40 AM7/26/14
to google-adm...@googlegroups.com
Hi Eric,
How much size will be increased usually when you don't use interstitial = null?

Regards,
Venkat.

Eric Leichtenschlag

unread,
Jul 28, 2014, 8:14:30 PM7/28/14
to google-adm...@googlegroups.com
Here's what I'm getting from the interstitial sample app on a Nexus 5. When I checked today, there wasn't any significant difference when setting to null.

Before request: 34670 KB
After closing interstitiall: 53964 KB

So also about 20 MB...

Venkat Google Account

unread,
Jul 29, 2014, 12:36:29 AM7/29/14
to google-adm...@googlegroups.com
How to garbage collect that 20 MB? Is it a new bug in Google Play services?

Regards,
Venkat.

--

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

Eric Leichtenschlag

unread,
Jul 29, 2014, 2:34:28 PM7/29/14
to google-adm...@googlegroups.com
Looks like that's the overhead of spinning up a WebView for the first time. If I run:

WebView webView = new WebView(this);
webView
.loadUrl("http://www.google.com"); // or some website that doesn't redirect
webView
= null;

in my app, memory also increases by 20 MB. Since the SDK uses WebView's, and I imagine most if not all ads SDKs do, there isn't much that can be done.
To unsubscribe from this group and all its topics, send an email to google-admob-ads-sdk+unsub...@googlegroups.com.

Venkatramanan

unread,
Aug 2, 2014, 12:08:04 AM8/2/14
to google-adm...@googlegroups.com
If it is the case, if users have more apps which uses admob then their phone's RAM usage will be high and it will cause issues for them and us. It is a serious issue. Isn't it?

Regards,
Venkat.

Eric Leichtenschlag

unread,
Aug 3, 2014, 1:33:29 PM8/3/14
to google-adm...@googlegroups.com
I haven't tried what happens when you open up several apps with AdMob at the same time. But the same behavior would happen with everyone app uses a WebView to to send or load data to a backend.

Android handles memory issues by stopping applications running in the background when memory is low.
Reply all
Reply to author
Forward
0 new messages