11

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?

Community
  • 1
  • 1
venkat
  • 517
  • 1
  • 8
  • 29

2 Answers2

13

I ran to this issue today, finally I have a fix: In your activity, in onDestroy():

  • If there is an AdView, remove it from your layout, set the AdView to null.
  • Set the all the AdListeners to null.

     @Override 
      public void onDestroy(){
          super.onDestroy();
          mMainView.removeView(adView);
          adView = null;
          interstitial.setAdListener(null);
      }
    

    In your case, maybe set AdListener to null is enough.

P/S:

Because leaks happen in so many ways, just in case someone misses this: remember to initialize the Interstitial using Context.getApplicationContext() (like what OP did), not by your Activity - which absolutely creates a leak - because Interstitial now seems to hold a reference to your Activity, and it never leaves, so never let your Activity go ...

Interstitial interstitial = new Interstitial(getApplicationContext())
DuDu
  • 341
  • 3
  • 8
  • Trying this, it seems to solve the issue since there are no longer multiple instances of the activity the interstitial was launched from. However, something is still not right. If left overnight to sit and load ads it still accumulates memory somewhere. I think it's a google sdk class but maybe the fault is still in the app somewhere. – Sojurn Jan 21 '15 at 04:48
  • This does not prevent AdActivity object from leaking. There will be multiple copies of AdActivity with each Interstitial shown... – frankish Mar 25 '15 at 11:31
  • @frankish That's the AdMob's side of this issue, we do not have any control over AdActivity. My approach is try as hard as we can in our side where we have the full power, each of our own-written activity. How about your issue, is it ok now? – DuDu Mar 25 '15 at 16:08
  • 1
    setting Interstitial's context to app context lets GC remove my MainActivity. However, knowing that all the ad contents are still in memory and it grows incrementaly does really bother me :| – frankish Mar 26 '15 at 12:19
  • The `getApplicationContext()` tip is what worked for me. – Shaun Dychko Aug 05 '17 at 00:56
  • This is not a solution! AdActivity will still holds your activity. – Berkay Turancı Sep 10 '17 at 13:27
1

I have managed to fix this issue by running the Ad activity in another process. I guess due to some reasons, android keep the activities in process memory for more time than it is needed. Hope it will help someone to fix this issue.

venkat
  • 517
  • 1
  • 8
  • 29
  • Do you have an example? I'm trying everything already with the same problem but can't find it. Did you make a new class for this? – Riverside Sep 28 '14 at 11:52
  • 1
    Use android:process attribute to separate the activities to run in different process. That solved my issue. Use it appropriately for your project. – venkat Sep 28 '14 at 20:06
  • Use DDMS view in eclipse to analyse the memory leaked objects. It may give some clue about your issue. – venkat Feb 19 '15 at 18:38