2

My Activity is still in the memory after running "finish()" and OnDestroy(). It is not garbage collected.

After days of analysis with Memory Profiler I have isolated the issue in the RewardedAd.

Basically, if I comment the entries in createAndLoadRewardedAd() method, where rewardedAd is initialized, the activity is properly destroyed:

private RewardedAd createAndLoadRewardedAd() {

    if (1==1) return null;  //IF THIS LINE IS COMMENTED, THERE WILL BE MEMORY LEAK!!!

    RewardedAd rewardedAd = new RewardedAd(this,
            getResources().getString(R.string.rewards_ad_unit_id));
    RewardedAdLoadCallback adLoadCallback = new RewardedAdLoadCallback() {
        @Override
        public void onRewardedAdLoaded() {
            // Ad successfully loaded.                
        }

        @Override
        public void onRewardedAdFailedToLoad(int errorCode) {
            // Ad failed to load.                
        }
    };
    rewardedAd.loadAd(new AdRequest.Builder().build(), adLoadCallback);
    return rewardedAd;
}

I have implemented rewardedAd as stated in the official documentation: https://developers.google.com/admob/android/rewarded-ads

Unfortunately, this document does not provide instructions to disable/destroy/nullify the related objects.

I'm setting rewardedAd to null in onDestroy, but this is not enough.

NOTE: I have posted same question into Google Mobile Ads SDK Developers forum https://groups.google.com/forum/#!category-topic/google-admob-ads-sdk/android/lPGZq54z53g

Pablo Alfonso
  • 2,317
  • 1
  • 18
  • 24
  • 1
    A quick link to the first "resolution" from the Google Mobile Ads SDK team: "the leak is temporary". https://groups.google.com/g/google-admob-ads-sdk/c/lPGZq54z53g/m/aUgbCckOBwAJ However, on some devices OP noticed that the leak lasts forever, so if anyone else with memory heavy Activities is reading this, take extra care to release resources you have control over to minimize the risk of an OOM Exception. That is the workaround that OP used eventually as well. – Ma3x Sep 23 '20 at 11:06
  • 1
    OP: since you did mention that it took you months to figure out what was causing your OOM problems (and days after you started to look into ads/sdks), consider adding LeakCanary to you dev builds (it really is a one-liner, not kidding). I got the report about the leak right after I integrated the ads, and when I saw that the reference was held by the SDK code I started searching and I found this SO question. – Ma3x Sep 23 '20 at 11:11

0 Answers0