我有关于内存泄漏Interstitial
ads of AdMob
with AdActivity
目的。每当广告出现时,AdActivity
内存中的对象计数增加 1。我通过以下方式检查所有更改MAT
明确后GC
来电。我使用所有东西的最新版本。
起初,我认为这与我实现 UI 或项目的方式有关,但创建一个新的空项目会显示相同的泄漏。
之前也出现过这种泄露Admob SDK
(Google Play 服务)现在它也存在于 7.0 版本中。
我看到人们试图通过创建一个SingleInstance
空活动只是为了显示并设置为插页式广告的上下文。我尝试了所有方法,但它们对我的情况不起作用。有些确实有帮助,但由于我的应用程序的流程,我什至无法使用它。launchMode
Android 有局限性,它对我的情况没有帮助。
我已经通知了 AdMob 团队,但即使他们修复了这个问题,似乎也不会在短时间内发生,因为他们刚刚发布了 7.0 版 SDK。
我不明白为什么其他人不报告这样的泄漏。这不能只是我的特例,因为它甚至发生在样本或默认模板上。如果有人以某种方式解决了这个问题(包括丑陋的反射黑客),请分享您的经验。我已经为此工作了几个月!真的吗!
Android 清单:
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="22" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:name="MyApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />
</application>
主要活动:
package com.example.leaktest1;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
private InterstitialAd interstitial=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
interstitial= new InterstitialAd(getApplicationContext());
interstitial.setAdUnitId("YOUR-ADD-ID");
AdRequest adRequest2 = new AdRequest.Builder()
// .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
interstitial.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
if(interstitial.isLoaded()){
interstitial.show();
}
}
});
interstitial.loadAd(adRequest2);
}
@Override
protected void onDestroy() {
if(interstitial!=null){
interstitial.setAdListener(null);
interstitial=null;
}
super.onDestroy();
}
}
Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.leaktest1.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
- 启动应用程序。显示广告后,通过后按将其关闭,然后再次点击后退按钮以退出应用程序。
- 应用程序仍将在内存中,但活动已消失。现在触摸应用程序图标再次启动活动,它将再次显示广告,像以前一样退出。
- Cause GC multiple times and get heap dump. You will see that there are 2 AdActivity objects (and also many other related objects). It will continue to grow according to the number of shown ads.
以下也不起作用(它仍然泄漏):
/*
interstitial.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
}
});*/
interstitial.loadAd(adRequest2);
Runnable r=new Runnable() {
@Override
public void run() {
if(interstitial.isLoaded()){
interstitial.show();
}
}};
new Handler().postDelayed(r,10000);
将代码放入按钮中也不起作用(它仍然泄漏):
Button b = new Button(this);
b.setText("Touch me");
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(interstitial.isLoaded()){
interstitial.show();
}
}
});
ViewGroup v = (ViewGroup) this.findViewById(android.R.id.content);
v.addView(b);
泄露平台及异常:此泄漏发生在三星和华硕的各种未修改的设备上(原厂固件)Android系统从2.3到4.4。它也会发生在 Android 模拟器的任何设置上。
(此泄漏不会出现在 root Cyanogenmod (Galaxy S3) Android 4.4.4 上)
UPDATE
The leak does not vanish if I use Activity
context instead of Application
context. It also causes Activity
leak too.
![enter image description here](https://i.stack.imgur.com/Qvvnn.png)
![enter image description here](https://i.stack.imgur.com/SZcFi.png)
![enter image description here](https://i.stack.imgur.com/eKxzA.png)