安卓4.1.1 Admob的有内存泄漏(见附件测试项目)。原因/修复/解决? [英] Android Admob 4.1.1 has a memory leak (see attached test project). Cause/fix/work around?
问题描述
我有一个测试项目,这表明造成Admob的4.1.1内存泄漏。
I have a test project which demonstrates a memory leak caused by Admob 4.1.1.
基本的情况是,我有两个活动,我来回切换主,子活动多次之间。我再切换到DDMS,并迫使一些选区。然后转储HPROF,并期待在过滤通过com.test的historgram *看看有多少主副行为的情况也有。我直方图的截图附:
The basic scenario is I have two activities and I switch back and forth between the main and the sub activity several times. I then switch to DDMS and force a few GCs. Then dump the HPROF and look at the historgram filtered by com.test* to see how many instances of the main and sub activity there are. My screenshots of the histogram are attached:
一个泄漏!
A leak!
我然后注释掉在XML中的广告,并重新运行,并没有泄漏:
I then commented out the ads in the xml and reran and there were no leaks:
没有现在泄漏
No leak now
我已经找到了一些相关的职位,Admob的泄漏,如在这里:安卓的AdMob导致内存泄漏?
I have found some relevant posts to Admob leaks such as here: Android AdMob causes memory leak?
下面是一个东西我试图解决该问题的列表:
Here is a list of things I have tried to resolve the issue:
- 在等待一定的时间,然后强制GC
- 请不要加载OnCreate中的广告,但产生一个线程等待,然后将其加载
- 试过的AdMob一个previous版本(不写由谷歌的)
- 在调用adView.destroy()中的onDestroy()活性的方法
- 从该<一的解除绑定href="http://1gravity.com/index.php?option=com_content&view=article&id=71%3aandroid-and-memory-leaks&catid=37%3aandroid&Itemid=59"相对=nofollow>链接
- Wait some amount of time and then force gc
- Do not load the ad in oncreate but spawn a thread to wait then load it
- Tried a previous version of Admob (the one not written by Google)
- Called adView.destroy() in the onDestroy() activity's method
- The unbinding from this link
显然没有这些东西帮助。
Obviously none of these things helped.
下面是测试项目中,我写道:
Here is the test project I wrote:
(请务必在运行测试项目设置自己的发布者ID)
(Be sure to set your own publisher id when you run the test project)
如果它的确与众不同,我测试功能对我SGS2用的CyanogenMod ROM。
If it makes a difference I'm tesing on my SGS2 with Cyanogenmod ROM.
这是否发生了其他人,当他们跑这个项目?
Does this happen for other people when they run this project?
有谁知道原因修补程序或解决方法吗?
Does anyone know the cause a fix or a workaround?
感谢
推荐答案
我的应用程序使用了80%的允许16Mb的,并在每个方向变化AD浏览报泄漏(由于Android坏,再现了整个活动的话)。结果我出内存后十几方向改变给了我可怕的:
My app uses 80% of the allowed 16Mb and the AdView leaks at every orientation change (since android destroys and recreates the whole activity then). As a result I am out of memory after a dozen or so orientation changes giving me the dreaded:
十月10日至8号:14:47.178:ERROR / dalvikvm堆(2876):1440000字节的外部分配太大,此过程
10-08 10:14:47.178: ERROR/dalvikvm-heap(2876): 1440000-byte external allocation too large for this process.
十月10日至8号:14:47.178:ERROR / dalvikvm(2876):内存:堆大小= 5191KB,分配= 2877KB,位图大小= 18675KB
10-08 10:14:47.178: ERROR/dalvikvm(2876): Out of memory: Heap Size=5191KB, Allocated=2877KB, Bitmap Size=18675KB
十月10日至8号:14:47.178:ERROR / GraphicsJNI(2876):虚拟机不会让我们分配1440000字节
10-08 10:14:47.178: ERROR/GraphicsJNI(2876): VM won't let us allocate 1440000 bytes
或类似的。
在内存的增加可以很容易地通过做调试运行和打开窗口>打开透视图>其它> DDMS,点击更新堆图标,并做了原因GC可见月食。检查恕我直言,最简单的方法是#Objects。如果方向已经从纵向改为横向和背部,物体的数量应该是完全一样的(并没有AD浏览报是)。
The increase in memory can easily be seen in eclipse by doing a debug run and opening Window > Open perspective > Other > DDMS, clicking the "update heap" icon and doing a Cause GC. The easiest way to check imho is the #Objects. If the orientation has changed from portrait to landscape and back, the number of Objects should be exactly the same (and without AdView it is).
我解决了内存泄漏通过AD浏览报静态
I work around the memory leak by making the AdView static
private static AdView mAdView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(mAdView == null)
{
mAdView = new AdView(this, AdSize.BANNER, ADMOB_PUBLISHER_ID);
}
}
而不是调用destroy
and not calling the destroy
@Override
public void onDestroy() {
super.onDestroy();
//mAdView.destroyDrawingCache();
//mAdView.destroy();
//mAdView = null;
}
目前至少每方向变化之间的prevents内存泄漏。
At least this prevents memory leaks between every orientation change.
此外,我用了之后设置请求无效。不知道有没有什么帮助。
Also I set request to null after using it. Don't know if that helps.
AdRequest request = new AdRequest();
mAdView.loadAd(request);
request = null;
这篇关于安卓4.1.1 Admob的有内存泄漏(见附件测试项目)。原因/修复/解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!