Android的 - 内存泄漏时动态生成用户界面与图像资源背景 [英] Android - Memory leak when dynamically building UI with image resource backgrounds

查看:93
本文介绍了Android的 - 内存泄漏时动态生成用户界面与图像资源背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我发誓正在泄漏内存的活动。该应用程序我工作做了很多处理图像,所以我不得不直接处理位图时要pretty的吝啬内存。我添加了一个活动,而现在,如果你使用这个新的活动,它基本上使我在与内存使用的边缘,我最终会把位图超过VM预算的异常。如果你从来没有推出这个活动,一切都顺利,因为它是previously。

我开始阅读关于内存泄漏,而且我认为我有一个类似的情况正在发生的事情的文章在Android文档描述。我动态创建了一堆图像视图,并从资源,增加了BackgroundDrawable并添加OnClickListener为好。我想我必须做一些清理活动时碰到的onPause在其生命周期,但我想知道具体是什么是正确的做法。

下面是code,应该表现出我与...对象

 的LinearLayout templateContainer;
    。
    。
    。
    ImageView的imgTemplate preVIEW =(ImageView的)item.findViewById(R.id.imgTemplate preVIEW);
    。
    。
    。
    imgTemplate preview.setBackgroundDrawable(getResources()getDrawable(previewId)。);
    imgTemplate preview.setOnClickListener(imgClick);
    templateContainer.addView(项目);
 

解决方案

如果你将要处理许多位图 S当他们不需要你应该积极地清除它们(的onPause C $ C>是一个好的开始)。

我记得很久以前关于的ImageView 和他们与缠绵引用它们的位图显示古怪行为的讨论。从我都记得的是,你应该从ImageView的删除当前上下文的所有引用,如果你要保持布局活着,但不希望泄露。删除onClick的监听器和位图。呼叫 .recycle()位图如果你要积极地释放内存。确保你没有任何静态字段与缠绵参考上下文或内部类的引用。

在code为Android的发射器也被在此提到的一个很好的参考,他们做了一些这些事情。开放源代码是你的朋友。

修改

找到罗曼家伙的文章。 Basicly他提到这部分的方式,虽然

  

这个例子是泄漏的语境中最简单的例子之一,你可以看到,我们的存储可绘回调设置在主屏幕的源$ C ​​$ C(寻找unbindDrawables()方法)工作围绕它当活动被销毁空。

现在我从来没有来管理这种类型的内存使用情况(还),所以在这里我建议在看<一href="http://android.git.kernel.org/?p=platform/packages/apps/Launcher.git;a=blob;f=src/com/android/launcher/LauncherModel.java;h=0ef2a806b767142b28b2ff3b37f21f4ca16c355d;hb=cupcake"相对=nofollow>主屏幕源了解更多详情。你会发现自己的解除绑定()上线620法

I have an Activity that I swear is leaking memory. The app I'm working on does a lot with images, so I've had to be pretty stingy with memory when working directly with Bitmaps. I added an Activity, and now if you use this new Activity it basically puts me over the edge with mem usage and I end up throwing the "Bitmap exceeds VM budget" exception. If you never launch this Activity, everything is smooth as it was previously.

I started reading about memory leaks, and I think that I have a similar situation to what is described in the article in the Android docs. I'm dynamically creating a bunch of image views and adding a BackgroundDrawable from the resources and adding an OnClickListener as well. I imagine I have to do some cleanup when the Activity hits onPause in its life cycle, but I'd like to know specifically what is the correct way.

Here is the code that should demonstrate the objects I'm working with...

    LinearLayout templateContainer;
    .
    .
    .
    ImageView imgTemplatePreview = (ImageView) item.findViewById(R.id.imgTemplatePreview);
    .
    .
    .
    imgTemplatePreview.setBackgroundDrawable(getResources().getDrawable(previewId));
    imgTemplatePreview.setOnClickListener(imgClick);
    templateContainer.addView(item); 

解决方案

Rich,

If you are going to be dealing with that many Bitmaps you should aggressively clean them up when they are not needed (onPause is a good start).

I remember a discussion from a long time ago about ImageViews and their odd behavior with lingering references to their displayed bitmaps. From what I do recall is that you should remove all references to the current context from the ImageView if you are going to keep the layout alive but don't want to leak. Remove the onClick listener and bitmap. Call .recycle() on the Bitmap if you want to aggressively free memory. Make sure you don't have any static fields with lingering references to the context or inner class references to it.

The code for Android's launcher was also mentioned during this as a good reference where they do some of these things. OpenSource is your friend.

EDIT

Found Romain Guys article. Basicly he mentions this part of the way though

This example is one of the simplest cases of leaking the Context and you can see how we worked around it in the Home screen’s source code (look for the unbindDrawables() method) by setting the stored drawables’ callbacks to null when the activity is destroyed.

Now I've never had to manage this type of memory usage (yet) so from here I suggest looking at The Home Screen Source for more details. You'll find their unbind() method on line 620

这篇关于Android的 - 内存泄漏时动态生成用户界面与图像资源背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆