问题描述
我已将 AdMob v4.1.0 集成到我的应用程序中,但它似乎导致了巨大的内存泄漏(很确定它已经在 4.0.4 上发生了).
I've integrated AdMob v4.1.0 into my application and it seems to have caused a huge memory leak (pretty sure that it already happened on 4.0.4).
为了隔离问题,我创建了一个具有空白线性布局的新项目,并向其中添加了 AdView(这实际上是从 AdMob 提供的示例代码中复制粘贴的).查看我的 main.xml、MainActivity.java 和清单内容:
In order to isolate the problem I created a new project with a blank linear layout and added the AdView to it (this is actually a copy&paste from the sample code provided by AdMob). See my main.xml, MainActivity.java and manifest content:
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/linearLayout">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
MainActivity.java:
MainActivity.java:
package AdsTry.main;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
private final int AD_VIEW_ID = 1000000;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Lookup R.layout.main
LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout);
// Create the adView
// Please replace MY_BANNER_UNIT_ID with your AdMob Publisher ID
AdView adView = new AdView(this, AdSize.BANNER, "MY_BANNER_UNIT_ID");
adView.setId(AD_VIEW_ID);
// Add the adView to it
layout.addView(adView);
// Initiate a generic request to load it with an ad
AdRequest request = new AdRequest();
adView.loadAd(request);
}
@Override
protected void onPause() {
Log.i("AdsTry", "onPause");
getAdView().stopLoading();
super.onPause();
}
@Override
protected void onDestroy() {
Log.i("AdsTry", "onDestroy");
getAdView().destroy();
super.onDestroy();
}
private AdView getAdView()
{
return (AdView) findViewById(AD_VIEW_ID);
}
}
清单:
<application android:icon="@drawable/icon" android:label="@string/app_name">
<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>
<!-- AdMobActivity definition -->
<activity android:name="com.google.ads.AdActivity"
android:configChanges="orientation|keyboard|keyboardHidden" />
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
这就是我所有的代码.
现在,当运行这个应用程序时,我可以看到 onPause 和 onDestory 都被调用,Activity 被终止,但问题是它永远不会被 GC 使用,因为它导致 InputMethodManager 持有对活动(见活动被销毁后从 HPROF 输出中截取的图像):
Now, when running this application, I can see that both onPause and onDestory are called and the Activity is terminated, BUT the problem is that it will never be available for the GC since it causes the InputMethodManager to hold a reference to the Activity (See image taken from HPROF output after the activity was destroyed):
一旦我删除了与 AdView 相关的代码(同样,这是此应用程序的唯一代码),问题就会消失:
Once I remove the AdView-related code (and again, this is the ONLY code of this application) the problem goes away:
还尝试从 onCreate 中删除所有代码并更新 main.xml 以包含以下内容(仍然得到相同的结果):
Also tried removing ALL the code from onCreate and updated the main.xml to contain the following (still get the same result):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linearLayout">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<com.google.ads.AdView
android:id="@+id/Ad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adUnitId="MY_ID"
ads:adSize="BANNER"
ads:loadAdOnCreate="true"/>
</LinearLayout>
有什么想法吗????
推荐答案
我正在使用play-services-ads:7.5.0"并且没有必要创建 de AdMobActivity.它的工作原理是:
I am using "play-services-ads:7.5.0" and it was not necesary to create de AdMobActivity. It worked by:
动态创建 adView
Creating adView dinamically
mAdView = new AdView(getApplicationContext(), AdSize.BANNER, banner_ad_unit_id); mAdsContainer.addView(mAdView);
在销毁和销毁 adView 时从 linearLayout 中删除所有视图
Removing all views from linearLayout on destroy and destroying adView
mAdView.setAdListener(null);
mAdsContainer.removeAllViews();
mAdView.destroy();
不幸的是,Interstitial 仍然泄漏
Unfortunatelly Interstitial still leaks
这篇关于Android AdMob 导致内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!