Android的Admob的间质内存泄漏 [英] Android Admob Interstitial Memory leak
问题描述
我想在一些活动结束,显示间隙。问题是间隙似乎prevent活动被垃圾收集,造成内存不足的例外。请问该如何解决呢?先谢谢了。
公共类AdActivity扩展FragmentActivity {
// ...
保护InterstitialAd间质性;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
// ...
//创建间隙。
间质性=新InterstitialAd(本);
interstitial.setAdUnitId(INTERSTITIAL_UNIT_ID);
//创建广告请求。
AdRequest adRequest2 =新AdRequest.Builder()。addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice(DEVICEID)
。建立();
//开始装货间隙。
interstitial.loadAd(adRequest2);
}
@覆盖
公共无效的onPause(){
super.onPause();
displayInterstitial();
}
公共无效displayInterstitial(){
如果(interstitial.isLoaded()&安培;&安培; System.currentTimeMillis的()> = lastInterstitial +时间滞后* 1000){
lastInterstitial = System.currentTimeMillis的();
interstitial.show();
}
}
和我用它喜欢的:
公共类ActivityA扩展AdActivity {// ...
}
好吧,我似乎被改变有固定
间质性=新InterstitialAd(本);
到
间质性=新InterstitialAd(getApplicationContext());
我不完全理解在Java / Android的内存管理,但我想,怎么回事是因为活动的参考间质和质的引用,这样的活动既没有得到垃圾收集。传递中的应用程序上下文,而不是活动范围内prevents这个循环依赖和解决的问题。希望这可以帮助别人。:D
I'm trying to show interstitials at the end of some Activities. The problem is the interstitials seem to prevent the Activities from being garbage collected causing an out-of-memory exception. How do i resolve this? Thanks in advance.
public class AdActivity extends FragmentActivity{
//...
protected InterstitialAd interstitial;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//...
// Create the interstitial.
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId(INTERSTITIAL_UNIT_ID);
// Create ad request.
AdRequest adRequest2 = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice(deviceId)
.build();
// Begin loading interstitial.
interstitial.loadAd(adRequest2);
}
@Override
public void onPause() {
super.onPause();
displayInterstitial();
}
public void displayInterstitial() {
if (interstitial.isLoaded() && System.currentTimeMillis() >= lastInterstitial + timeLag * 1000) {
lastInterstitial = System.currentTimeMillis();
interstitial.show();
}
}
And i used it like:
public class ActivityA extends AdActivity{ //...
}
Ok i seem to have fixed it by changing
interstitial = new InterstitialAd(this);
to
interstitial = new InterstitialAd(getApplicationContext());
I don't completely understand memory management in java/android but I think whats going on is because the Activity references interstitial and interstitial has a reference to Activity so neither gets garbage-collected. Passing in the application context rather than the Activity context prevents this cycle dependency and solves the problems. Hope this helps someone :D.
这篇关于Android的Admob的间质内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!