`
baobaoupup
  • 浏览: 471416 次
文章分类
社区版块
存档分类
最新评论

内存泄露

 
阅读更多

在T-Mobile G1上,android application被限制到16M堆内存。

越多的application保存在内存中,那么在他们之间切换的时候,速度就会更快。

a Context 能被在很多方面应用,但是最多的是加载访问资源,这就是为啥widget接受一个a Context 参数在构造函数里。

在一个规则的application中,你通常用俩中Context,activity和application。

当屏幕方向转变时,默认摧毁当前的activity 创建一个新的,android将重新加载application的ui。

如果你有一个大的bitmap你不想每次转变屏幕时都加载,如果你用一个static去总是保持它

private static Drawable sBackground ;

@Override

protected void onCreate ( Bundle state )

{ super . onCreate ( state );

TextView label = new TextView ( this );

label . setText ( "Leaks are bad" );

if ( sBackground == null ) {

sBackground = getDrawable ( R . drawable . large_bitmap );

}

label . setBackgroundDrawable ( sBackground );

setContentView ( label ); }

代码很快,但是也很错。他泄露了这第一个activity,当一个drawable被 attached to a view,这个VIEW被设置作为一个callback在这个drawable,在上述代码中,这意味着这个drawable有一个引用在textview,而这个textview本身有一个引用在这个activity上,而这个activity依次有许多他自身的引用。你可以参看home screen源代码通过设置drawble的callback为空当activity被摧毁时。

/** * Remove the callback for the cached drawables or we leak the previous* Home screen on orientation change. */


  private void unbindDrawables(ArrayList<ItemInfo> desktopItems) {



if (desktopItems != null) {



final int count = desktopItems.size();



for (int i = 0; i < count; i++) {



ItemInfo item = desktopItems.get(i);



switch (item.itemType) {



case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:



case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:



((ApplicationInfo)item).icon.setCallback(null);



break;



}



}



}



}







俩中避免与context相关联的内存泄露。The most obvious one is to avoid escaping the context outside of its own scope.
上述例子展示了一个静态引用的例子。但是内部类和他们的隐士的外部引用能是同样危险的。
这第二个解答是用application context。这个context将活着和你的application一样长,并不依赖这activity的生命周期。
如果你计划保持长引用的对象,他需要一个context,记者用application的。通过Context.getApplicationContext()
or Activity.getApplication()


.







  • Do not keep long-lived references to a context-activity (a reference to an activity should have the same life cycle as the activity itself)
  • Try using the context-application instead of a context-activity
  • Avoid non-static inner classes in an activity if you don’t control their life cycle, use a static inner class and make a weak reference to the activity inside

FAQ:

1.If Drawable just a member field and not static - it’s fine right?

An instance variable will not cause any problem indeed.

2.public void setBackgroundDrawable(Drawable d) {

if (d != null) {

d.setCallback(this);

}

}

从这个代码看出,上述例子里the setBackgroundDrawable reset the drawable’s callback after the activity reloaded.

when the setBackgroundDrawable() is called, the Drawable has setCallback() invoked, which will overwrite the current callback, therefore no memory leak?

  1. Romain Guy

    Justin,

    If you always set it on a View, then it’s not an issue (well, except that you kept a hold onto the previous Context for longer than needed.) It can become very problematic for an application in which you use the drawables in a ListView for instance: as long as the drawable is not re-bound to a View, you keep the old Context and all the resources and Views. It’s much safer to not rely on that at all.

  2. 上述说了你不可能总是记者re-bound到一个VIEW上,一旦你忘了,就会泄露。
  3. 14 Romain Guy

    There’s also the situation of when your Activity is destroyed but not recreated (it can happen for several reasons.) If the process is still there, your static fields also are and you are leaking the Activity until it is recreated.

  4. 这段话说了如果你的activity被摧毁但是没被创建,如果你的进程仍旧在哪,你的静态字段也在哪,那么你正在泄露。

分享到:
评论

相关推荐

    Android webview 内存泄露的解决方法

    Android webview 内存泄露的解决方法 最近在activity嵌套webview显示大量图文发现APP内存一直在涨,没法释放内存,查了很多资料,大概是webview的一个BUG,引用了activity导致内存泄漏,所以就尝试传递...

    GDB查找内存泄露

    后台程序中经常会存在一部分内存泄露,但是不能很好的定位造成内存泄露的代码,并进行修改,内存泄露不进行修改就会造成程序运行时占用的内存不断升高,逐渐的导致系统的不稳定,现将前一段时间通过GDB调试工具查找内存...

    vld2.5 C++内存泄露检测工具

    关键词:内存泄露,visual leak detector,vld VLD是一款用于VisualC++的免费内存泄漏检查工具。可以在codeproject.com网站上找到,相比其它的内存泄漏哦给你根据,他在检查内存泄漏的同事,还具有如下特点: 1) ...

    C++内存泄露检测器

    C++ 内存 泄露 检测器 对于一个c/c++程序员来说,内存泄漏是一个常见的也是令人头疼的问题。已经有许多技术被研究出来以应对这个问题

    linux 内核 内存泄露检测

    linux 内核 内存泄露检测 linux 内核 内存泄露检测 linux 内核 内存泄露检测 linux 内核 内存泄露检测 linux 内核 内存泄露检测 linux 内核 内存泄露检测 linux 内核 内存泄露检测

    几个内存泄漏的例子

    几个内存泄漏的例子  new和delete要成对使用  new和delete要匹配 经常看到一些C++方面的书籍中这样提及到内存泄漏问题,这样的说法的意思是比较明白,但对于初学C++程序员还是很难掌握,所以下面举几个反面的...

    Linux 内存泄露查找

    linux 内存泄露排查文档。 介绍mtrace的使用。根据实际问题介绍如果定位问题。

    opencv3和opencv4多线程内存泄漏问题

    opencv3和opencv4多线程内存泄漏问题:以cv::resize函数测试结果为例。 使用中可修复或者可避免内存泄漏:1)使用opencv2的版本;2)在代码中设置修复该问题.

    Windows内存泄漏排查工具

    LeakDiag和LDGrapher,windows内存泄漏排查工具,用于进行一些泄漏位置的查找以及可视化展示。

    windows下c++内存泄露检测工具使用方

    windows下c++内存泄露检测工具使用方windows下c++内存泄露检测工具使用方

    electron-vue开发环境内存泄漏问题汇总.docx

    electron-vue开发环境内存泄漏问题汇总.docx

    JS内存泄漏检测工具

    JS内存泄漏检测工具:IEJSLeaksDetector

    推荐4款linux下的c语言内存泄漏检测工具.zip

    推荐4款linux下的检测c语言编写的程序的内存泄漏工具 C语言和其他语言相比最大的特色就是能够操作内存 但是最常犯的错误也是内存泄漏(管杀不管埋) 所以我们需要用一些工具来帮助我们检测是否存在内存泄漏,存在多少 ...

    JProfiler对应用服务器内存泄漏问题诊断解决方案.docx

    JProfiler对应用服务器内存泄漏问题诊断解决方案

    linux下检查内存泄漏的工具+例子

    介绍了使用内存泄漏检测工具valgrind的安装和使用,内附安装包,测试程序,使用说明文档。

    freertos内存泄漏检测代码(ESP32)

    用于freertos嵌入式实时系统内存泄漏检测,可以实时查看内存状况,具体平台是ESP32

    Android处理内存泄漏的代码例子

    Android处理内存泄漏的代码例子。用于演示避免内存泄漏的几种方法,包括:关闭游标、重用适配、回收图像、注销监听、释放引用。

    OpenCV中的内存泄露问题(cvLoadImage,cvCloneImage)【转】

    OpenCV中的内存泄露问题(cvLoadImage,cvCloneImage) 在利用opencv时,有时候即使你觉得释放了资源,依然会造成内存泄露,原因究竟何在,这里给你答案!

    JVisualVM简介与内存泄漏实战分析

    JVisualVM简介与内存泄漏实战分析,如何分析内存溢出,定位内存溢出问题

    内存泄露静态检测模型

    摘 要 内存泄漏故障是程序中某处申请的内存空间,没有释放或没有完全释放或多次释放,是程序中常见的故障, 极易导致系统崩溃。从面向具体错误的测试思想出发,...关键词 内存泄露,区间运算,静态测试,故障模型,别名分析

Global site tag (gtag.js) - Google Analytics