【发布时间】:2011-09-03 04:15:49
【问题描述】:
我已将 AdMob v4.1.0 集成到我的应用程序中,它似乎导致了巨大的内存泄漏(很确定它已经发生在 4.0.4 上)。
为了隔离问题,我创建了一个带有空白线性布局的新项目并将 AdView 添加到其中(这实际上是 AdMob 提供的示例代码的复制和粘贴)。查看我的 main.xml、MainActivity.java 和清单内容:
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:
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 输出中获取的图像):
一旦我删除了与 AdView 相关的代码(同样,这是此应用程序的唯一代码),问题就消失了:
编辑: 还尝试从 onCreate 中删除所有代码并更新 main.xml 以包含以下内容(仍然得到相同的结果):
<?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>
有什么想法吗????
【问题讨论】:
-
还没有尝试过,但我在 XDA 上得到了回复,说版本 4.1.1 应该可以解决这个问题。 forum.xda-developers.com/showthread.php?t=1077698
-
我一直在我的项目中尝试使用 4.1.1,但我仍然遇到 Activity 泄漏。如果我删除 Admob,泄漏就会消失。如果我恢复到旧版本的 Admob,我仍然会遇到问题。如果我在 onDestroy 方法中调用 adView.onDestroy() 它没有帮助。如果我等待半小时并按 GC 数百次,它没有帮助。您是否找到任何解决方案或解决方法?我完全被困住了:(
-
没有适合我的解决方案。最终删除了广告。只是不值得......
-
我不确定这是否有帮助,但我在 Android 支持网站上创建了一个错误。 Admob 目前由 Google 持有,因此他们应该对此有所作为。我很惊讶这个错误已经不存在了。 ISSUE 59627
标签: android memory memory-leaks admob