【问题标题】:Memory goes on Increasing each time the interstitial ad is display in unity 5每次显示插页式广告时,内存都会增加 5
【发布时间】:2016-10-06 04:20:21
【问题描述】:

我正在使用 google admob 在 unity 5 中显示插页式广告。我从 github 下载了 googleads mobile unity 包和 googlemobileadssdkios。我在 Xcode 中构建并运行游戏,它显示横幅广告和插页式广告。每次加载和关闭广告时,我都使用调试导航器查看内存消耗。我注意到每次加载插页式广告时内存消耗都会增加。这意味着关闭插页式广告时不会释放内存。

我对从 github 下载的源代码没有任何更改。如果内存以这种模式增加,应用程序将由于内存不足而终止。那么,插屏广告关闭后如何释放内存呢?

【问题讨论】:

  • 请指出代码源,大部分需要通过Dispose或Finalizer Invocation显式释放

标签: c# xcode unity3d


【解决方案1】:

问题可能在于您如何使用InterstitialAd API。使用不当会导致内存泄漏。

如果您在函数内创建新的插页式实例并且该实例变量被声明为局部变量,则必须在函数结束之前或在失去对插页式的引用之前调用插页式类的Destroy函数插页式类。

例如:

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
}

现在,如果您将 InterstitialAd 实例作为全局变量,并且其中的脚本正在被另一个脚本破坏,则您必须在 OnDestroy 函数中也破坏 InterstitialAd 实例。

private InterstitialAd interstitial;

void showAdFunction()
{

    interstitial = new InterstitialAd(adUnitId);
    AdRequest request = new AdRequest.Builder().Build();
    //....   
    interstitial.LoadAd(request);
}

void OnDestroy()
{
    interstitial.Destroy(); //Destroy
}

【讨论】:

  • 谢谢。正如你所说,我在不同的地方调用 interstitial.Destroy() 函数,例如 showInterstitial()、HandleAdFailedToLoad()、HandleAdClosed()、HandleAdLeftApplication()。它对我有用。
  • 不错。很高兴我能提供帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多