You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

升级Google Ads库至21.5.0后Android应用内存泄漏求助

谷歌广告库21.5.0版本插屏广告内存泄漏问题

背景

今日将广告库升级至:

implementation 'com.google.android.gms:play-services-ads:21.5.0'

仅使用插屏广告(Interstitial Ads),但通过Leak Canary检测到内存泄漏问题。已在销毁Activity时对所有相关广告对象做了处理:将对象置为null、监听器置为null、调用setFullScreenContentCallback(null)等。

LeakCanary泄漏检测日志

┬───
│ GC Root: Input or output parameters in native code
│
├─ android.os.MessageQueue instance
│    Leaking: NO (MessageQueue#mQuitting is false)
│    HandlerThread: "ExoPlayerImplInternal:Handler"
│    ↓ MessageQueue[0]
│                  ~~~
├─ android.os.Message instance
│    Leaking: UNKNOWN
│    Retaining 2.5 MB in 605 objects
│    Message.what = 2
│    Message.when = 507477121 (661 ms after heap dump)
│    Message.obj = null
│    Message.callback = null
│    Message.target = instance @348531240 of android.os.Handler
│    ↓ Message.target
│              ~~~~~~
├─ android.os.Handler instance
│    Leaking: UNKNOWN
│    Retaining 2.5 MB in 604 objects
│    ↓ Handler.mCallback
│              ~~~~~~~~~
├─ com.google.android.gms.internal.ads.zzatb instance
│    Leaking: UNKNOWN
│    Retaining 2.5 MB in 603 objects
│    ↓ zzatb.zzm
│            ~~~
├─ com.google.android.gms.internal.ads.zzaup instance
│    Leaking: UNKNOWN
│    Retaining 22.1 kB in 81 objects
│    ↓ zzaup.zzb
│            ~~~
├─ com.google.android.gms.internal.ads.zzatz instance
│    Leaking: UNKNOWN
│    Retaining 16 B in 1 objects
│    ↓ zzatz.zzb
│            ~~~
├─ com.google.android.gms.internal.ads.zzclf instance
│    Leaking: UNKNOWN
│    Retaining 346 B in 10 objects
│    zzd instance of com.google.android.gms.internal.ads.zzcpc, wrapping
│    androidx.multidex.MultiDexApplication
│    ↓ zzclf.zzd
│            ~~~
├─ com.google.android.gms.internal.ads.zzcpc instance
│    Leaking: UNKNOWN
│    Retaining 24 B in 1 objects
│    zza instance of com.xxxxx.xxxxx.CreateCardsActivity with
│    mDestroyed = true
│    zzb instance of androidx.multidex.MultiDexApplication
│    zzc instance of com.xxxxx.xxxxx.CreateCardsActivity with
│    mDestroyed = true
│    mBase instance of androidx.multidex.MultiDexApplication
│    zzcpc wraps an Application context
│    ↓ zzcpc.zza
│            ~~~
╰→ com.xxxx.xxxxxx.CreateCardsActivity instance
​     Leaking: YES (ObjectWatcher was watching this because 
     xxxx.xxxxx.CreateCardsActivity received Activity#onDestroy() callback and
​     Activity#mDestroyed is true)
​     Retaining 8.4 kB in 204 objects
​     key = 433e4c0b-dd8e-4321-a496-8a5a5542d28e
​     watchDurationMillis = 428997
​     retainedDurationMillis = 423991
​     mApplication instance of androidx.multidex.MultiDexApplication
​     mBase instance of androidx.appcompat.view.ContextThemeWrapper

METADATA

Build.VERSION.SDK_INT: 28
Build.MANUFACTURER: BLU
LeakCanary version: 2.10
App process name: com.xxx.xxxx
Class count: 20438
Instance count: 386631
Primitive array count: 252937
Object array count: 46275
Thread count: 148
Heap total bytes: 39734117
Bitmap count: 52
Bitmap total bytes: 29096284
Large bitmap count: 0
Large bitmap total bytes: 0
Db 1: open /com.google.android.
datatransport.events
Db 2: closed google_app_measurement_local.db
Db 3: open androidx.work.workdb
Stats: LruCache[maxSize=3000,hits=182820,misses=342787,hitRate=34%]
RandomAccess[bytes=16217423,reads=342787,travel=94405937828,range=40656354,size=
56429010]
Analysis duration: 149315 ms

当前实现代码

FullScreenContentCallback设置代码(在OnCreate()中)

FullScreenContentCallback fullScreenContentCallback = new FullScreenContentCallback() {
    @Override
    public void onAdDismissedFullScreenContent() {
        mAdManagerInterstitialAd.setFullScreenContentCallback(null);
        mAdManagerInterstitialAd = null;
    }
};

广告加载代码

AdManagerAdRequest adRequest = new AdManagerAdRequest.Builder().build();

AdManagerInterstitialAd.load(this, getResources().getString(R.string.interstitial_ad_unit_id), adRequest,
        new AdManagerAdLoadCallback() {
            @Override
            public void onAdLoaded(@NonNull AdManagerInterstitialAd interstitialAd) {
                mAdManagerInterstitialAd = interstitialAd;
                mAdManagerInterstitialAd.setFullScreenContentCallback(fullScreenContentCallback);
            }

            @Override
            public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                // Handle the error                                
                mAdManagerInterstitialAd = null;
            }
        }
);

已尝试的解决方案

  • 使用getApplicationContext()替代Activity上下文调用AdManagerInterstitialAd.load(),但问题仍未解决。

求助:可行的解决思路?


内容的提问来源于stack exchange,提问作者Pablo Alfonso

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 00:54:56