RewardedAd causes memory leak. How to disable RewardedAd when destroying the activity in Android?

759 Views Asked by At

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

0

There are 0 best solutions below