我也尝试使用
ApplicationContext
,但由于某些原因它仍然泄漏。
在这里找到了一个类似的问题 AdMob(SDK 7.0)Android上的AdActivity泄漏,但没有答案。
还尝试在onDestroy()中将广告监听器和广告设置为null,但是没有成功,活动仍然泄漏。
我的代码在onCreate()中被调用
private void refreshInterstitial(){
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("AD_ID");
mInterstitialAd.loadAd(new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).addTestDevice("877BCC97E130A0DC62B2E5770D854496").build());
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
mInterstitialAd.show();
}
@Override
public void onAdClosed() {
}
});
}
Leakcanary 泄漏跟踪
┬───
│ GC Root: Global variable in native code
│
├─ mx instance
│ Leaking: UNKNOWN
│ ↓ mx.a
│ ~
├─ com.google.android.gms.ads.internal.webview.w instance
│ Leaking: UNKNOWN
│ mContext instance of com.google.android.gms.ads.internal.webview.ay, not wrapping activity
│ View#mParent is null
│ View#mAttachInfo is null (view detached)
│ View.mWindowAttachCount = 1
│ ↓ w.a
│ ~
├─ com.google.android.gms.ads.internal.webview.aa instance
│ Leaking: YES (View detached and has parent)
│ mContext instance of com.google.android.gms.ads.internal.webview.ay, not wrapping activity
│ View#mParent is set
│ View#mAttachInfo is null (view detached)
│ View.mWindowAttachCount = 1
│ ↓ aa.mListenerInfo
├─ android.view.View$ListenerInfo instance
│ Leaking: YES (aa↑ is leaking)
│ ↓ View$ListenerInfo.mOnClickListener
├─ com.google.android.gms.ads.nonagon.ad.webview.f instance
│ Leaking: YES (aa↑ is leaking)
│ ↓ f.a
├─ com.google.android.gms.ads.nonagon.ad.webview.l instance
│ Leaking: YES (aa↑ is leaking)
│ ↓ l.e
├─ com.google.android.gms.ads.nonagon.ad.event.bs instance
│ Leaking: YES (aa↑ is leaking)
│ ↓ bs.a
├─ java.util.HashMap instance
│ Leaking: YES (aa↑ is leaking)
│ ↓ HashMap.table
├─ java.util.HashMap$Node[] array
│ Leaking: YES (aa↑ is leaking)
│ ↓ HashMap$Node[].[1]
├─ java.util.HashMap$Node instance
│ Leaking: YES (aa↑ is leaking)
│ ↓ HashMap$Node.key
├─ com.google.android.gms.ads.nonagon.shim.k instance
│ Leaking: YES (aa↑ is leaking)
│ ↓ k.a
├─ com.google.android.gms.ads.internal.client.ae instance
│ Leaking: YES (aa↑ is leaking)
│ ↓ ae.a
├─ com.google.android.gms.internal.ads.zzuc instance
│ Leaking: YES (aa↑ is leaking)
│ ↓ zzuc.zzcbw
├─ com.test.Activity$1 instance
│ Leaking: YES (aa↑ is leaking)
│ Anonymous subclass of com.google.android.gms.ads.AdListener
│ ↓ EqualizerActivity$1.this$0
╰→ com.test.Activity instance
Leaking: YES (ObjectWatcher was watching this because Activity received Activity#onDestroy() callback and Activity#mDestroyed is true)
key = 40a1eb8e-c9e6-4062-b5f7-053e642e812f
watchDurationMillis = 5288
retainedDurationMillis = 258
mInterstitialAd
变成本地变量吗? - Harry Timothy