转自

http://hi.baidu.com/justtmiss/item/c939cd293b5096fc51fd87fe

http://developer.android.com/resources/articles/avoiding-memory-leaks.html

Android applications are, at least on the T-Mobile G1, limitedto 16 MB of heap. It's both a lot of memory for a phone and yet verylittle for what some developers want to achieve. Even if you do notplan on using all of this memory, you should use as little as possibleto let other applications run without getting them killed. The moreapplications Android can keep in memory, the faster it will be for theuser to switch between his apps. As part of my job, I ran into memoryleaks issues in Android applications and they are most of the time dueto the same mistake: keeping a long-lived reference to a Context.

On Android, a Context is used for many operations but mostly to load and access resources. This is why all the widgets receive a Context parameter in their constructor. In a regular Android application, you usually have two kinds of Context, Activity and Application. It's usually the first one that the developer passes to classes and methods that need a Context:

@Override
protectedvoid onCreate(Bundle state){
  super.onCreate(state);
  
  TextView label =newTextView(this);
  label.setText("Leaks are bad");
  
  setContentView(label);
}

This means that views have a reference to the entire activity andtherefore to anything your activity is holding onto; usually the entireView hierarchy and all its resources. Therefore, if you leak the Context("leak" meaning you keep a reference to it thus preventing the GC fromcollecting it), you leak a lot of memory. Leaking an entire activitycan be really easy if you're not careful.

When the screen orientation changes the system will, by default,destroy the current activity and create a new one while preserving itsstate. In doing so, Android will reload the application's UI from theresources. Now imagine you wrote an application with a large bitmapthat you don't want to load on every rotation. The easiest way to keepit around and not having to reload it on every rotation is to keep in astatic field:

privatestaticDrawable sBackground;
  
@Override
protectedvoid onCreate(Bundle state){
  super.onCreate(state);
  
  TextView label =newTextView(this);
  label.setText("Leaks are bad");
  
  if(sBackground ==null){
    sBackground = getDrawable(R.drawable.large_bitmap);
  }
  label.setBackgroundDrawable(sBackground);
  
  setContentView(label);
}

This code is very fast and also very wrong; it leaks the first activity created upon the first screen orientation change. When aDrawable is attached to a view, the view is set as a callback on the drawable. In the code snippet above, this means the drawable has a reference to the TextView which itself has a reference to the activity (the Context) which in turns has references to pretty much anything (depending on your code.)

This example is one of the simplest cases of leaking the Context and you can see how we worked around it in the (look for the unbindDrawables() method) by setting the stored drawables' callbacks to null when the activity is destroyed. Interestingly enough, there are cases where you can create a chain of leaked contexts, and they are bad. They make you run out of memory rather quickly.

There are two easy ways to avoid context-related memory leaks. The most obvious one is to avoid escaping the context outside of its own scope. The example above showed the case of a static reference but inner classes and their implicit reference to the outer class can be equally dangerous. The second solution is to use the Application context. This context will live as long as your application is alive and does not depend on the activities life cycle. If you plan on keeping long-lived objects that need a context, remember the application object. You can obtain it easily by calling Context.getApplicationContext() or Activity.getApplication().

In summary, to avoid context-related memory leaks, remember the following:

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-activityAvoid 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. The solution to this issue is to use a static inner class with a WeakReference to the outer class, as done in and its W inner class for instanceA garbage collector is not an insurance against memory leaks

转载于:https://www.cnblogs.com/kitexy/archive/2013/06/06/3121042.html

android application 引起内存泄漏的原因相关推荐

  1. Android 中内存泄漏的原因和解决方案

    之前研究过一段时间关于 Android 内存泄漏的知识,大致了解了导致内存泄漏的一些原因,但是没有深入去探究,很多细节也理解的不够透彻,基本上处于一种似懂非懂的状态,最近又研究了一波,发现有很多新的收 ...

  2. 关于Android应用程序内存泄漏 你需要知道的一切

    关于Android应用程序内存泄漏 你需要知道的一切 原文:https://blog.aritraroy.in/everything-you-need-to-know-about-memory-lea ...

  3. Android中的内存泄漏

    ** Android中的内存泄漏 ** Android中的内存泄漏: 概念:程序在申请内存后,当该内存不需再使用但却无法被释放 & 归还给程序的现象,对应用程序的影响,容易使得应用程序发生内存 ...

  4. 基于Android Studio的内存泄漏检测与解决全攻略

    自从Google在2013年发布了Android Studio后,Android Studio凭借着自己良好的内存优化,酷炫的UI主题,强大的自动补全提示以及Gradle的编译支持正逐步取代Eclip ...

  5. Android常见的内存泄漏分析

    内存泄漏原因 当应用不需要在使用某个对象时候,忘记释放为其分配的内存,导致该对象仍然保持被引用状态(当对象拥有强引用,GC无法回收),从而导致内存泄漏. 常见的内存泄漏源头 泄漏的源头有很多,有开源的 ...

  6. 使用 Android Studio 检测内存泄漏与解决内存泄漏问题

    本文在腾讯技术推文上 修改 发布. http://wetest.qq.com/lab/view/63.html?from=ads_test2_qqtips&sessionUserType=BF ...

  7. android studio 解决内存泄漏

    自从Google在2013年发布了Android Studio后,Android Studio凭借着自己良好的内存优化,酷炫的UI主题,强大的自动补全提示以及Gradle的编译支持正逐步取代Eclip ...

  8. UWP开发入门(十六)——常见的内存泄漏的原因

    原文:UWP开发入门(十六)--常见的内存泄漏的原因 本篇借鉴了同事翔哥的劳动成果,在巨人的肩膀上把稿子又念了一遍. 内存泄漏的概念我这里就不说了,之前<UWP开发入门(十三)--用Diagno ...

  9. 谈谈android中的内存泄漏

    写在前面 内存泄漏实际上很多时候,对于开发者来说不容易引起重视.因为相对于crash来说,android中一两个地方发生内存泄漏的时候,对于整体没有特别严重的影响.但是我想说的是,当内存泄漏多的时候, ...

最新文章

  1. Js Call方法详解(js 的继承)
  2. 【转载】单片机的背后
  3. QT学习:网络应用开发练习(文件下载)
  4. MVC学习之分页 【转】
  5. [Java基础]Collection集合
  6. linux可以不用grub吗,既然不用Win了,那么GrubDOS也不用了。linux grub求指导
  7. 求一个向量变换为另一个向量的矩阵_OpenGL里旋转等变换矩阵为什么是4x4的矩阵...
  8. python startswitch_使用python esl 实现FreeSWITCH自动外呼
  9. mvc ajax返回整个页面跳转,在springmvc中的ajax发布调用之后,有什么方法可以将我的页面(jsp)重定向到另一个页面(jsp)...
  10. NGINX的奇淫技巧 —— 7. IF实现数学比较功能 (2)
  11. centos sftp客户端 c 源码_Redis第3课:如何使用 Redis客户端
  12. 自定义数字格式字符串_部分分隔符和条件格式
  13. .net 3.5 数据库开发 之 LINQ 上
  14. c语言三个学生每人四门,C语言一道题目,求教教3.统计一个班的学生成绩。要求程序具有如下功能:(1) 每个学生的学号和四门功课的成绩从键盘读入。...
  15. DeepFool论文翻译---DeepFool: a simple and accurate method to fool deep neural networks
  16. 2021年末大盘点。IT行业那些薪资高前景好的岗位,你知道几个?
  17. Android中验证姓名、身份证、银行卡、手机号(正则表达式校验)
  18. 输入一个字符串,字符串长度大于6,让黄灯长亮,否则一直闪烁
  19. 雇佣兵战斗力c语言原理,暗黑2单机弓箭亚马逊应该怎么选技能和雇佣兵
  20. 微信公众号官方文档入口

热门文章

  1. 第一次 C语言课程设计
  2. Python实现链表
  3. 两个函数式解决大数相加的方法
  4. Nginx+ThinkPHP不支持PathInfo的解决办法
  5. 为什么下一个SaaS公司绝不会效仿Salesforce?
  6. lodop打印技巧与注意事项
  7. 通过委派模式包装一个RxJavaCallAdapterFactory
  8. Java --循环截取字符串
  9. 文件翻译002片:Process Monitor帮助文档(Part 2)
  10. 如何在存储过程中得到被调用存储过程的结果集