Reputation: 389
I am using google admob for showing interstitial ads in unity 5. I download the googleads mobile unity package and googlemobileadssdkios from github. I build and run the game in Xcode it shows banner ads and interstitial ads. I am using debug navigator to view the memory consumption each time the ads is loaded and closed. I noticed that the memory consumption is increasing each time the interstitial ad is loaded. That means memory does not get free up while closing the interstitial ads.
I have change nothing in the source code downloaded from github. If memory goes increasing in this pattern, the application will be terminated due to insufficient memory. So, how to free up the memory after the interstitial ad is closed?
Upvotes: 2
Views: 1691
Reputation: 125305
The problem is likely how you use the InterstitialAd
API. You will get memory leak if not used correctly.
If you are creating new interstitial instance inside a function and the instance variable is declared as a local variable, you have to call the Destroy
function of the interstitial class before the end of the function or before you lose the reference to the interstitial class.
For example:
void showAdFunction()
{
InterstitialAd interstitial = new InterstitialAd(adUnitId);
AdRequest request = new AdRequest.Builder().Build();
//....
interstitial.LoadAd(request);
interstitial.Destroy(); //Must do this before losing the reference
}
Now, If the you have the InterstitialAd
instance as a global variable and the script it is inside is being destroyed by another script, you have to destroy the InterstitialAd
instance as well in the OnDestroy
function.
private InterstitialAd interstitial;
void showAdFunction()
{
interstitial = new InterstitialAd(adUnitId);
AdRequest request = new AdRequest.Builder().Build();
//....
interstitial.LoadAd(request);
}
void OnDestroy()
{
interstitial.Destroy(); //Destroy
}
Upvotes: 1