`
Teok
  • 浏览: 150732 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Android Memory Leaks

阅读更多
前一阵子,有一个bug是关于monkey测试到的memory leak,优先级是very high。负责解这个bug的同事分析了好几天,最后好像也没有解决。我一开始对如何查找memory leak也是非常模糊,总感觉无处下手。正好今天看到官方的一篇文章(avoiding-memory-leaks)来分析这个问题。

从文章角度看来,android的ml是有它自身的特点的:大量的context引用。我们知道,context是android很基础的类之一,有众多必须的api。于是引用来引用去,万一把某个context间接引用到static变量上去了,那不就有泄漏的危险了么。文章的那个例子就是说明这一点。

同时,主贴给出了4点以回避context相关的泄漏:

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


我自己琢磨出下面有关或者无关的几点:
1.从4点解决思路可以看出,文章强调对代码的控制,有些时候gc机制给程序员带来的负面影响就是容易忽视对代码生命周期的管理,尤其销毁(destroy)。至少我在看到这篇文章,给我很大启示。我想出现这种情况的原因,是因为java程序员对自身职责和语言职责界定不清楚导致,什么应该让语言本身去解决(如gc机制),什么应该程序员自己来。另一点是java程序员可能缺乏对控制语言的欲望,因为框架、标准库api实在太多了,熟悉api还来不及呢,更别说花更多的精力去探究api的设计意图和实现。要改变这种思维,只有慢慢地练,拿我自己来说,我现在开始督促自己多问为什么,多去追究设计者的意图,多从设计者的高度去体会这个api和体系结构,现在已经有了一些收获。
2.尽量application context是因为一个apk只有一个application context,但是经常会有多个activity context,尤其activity来回切换时,很容易出现某些activity context被间接引用而不能释放
3.过份依赖gc会死的很惨
4.只会点java不行,至少得了解c这样强控制类的语言的机制,要不等到出了这类问题,可能没有解决问题的思路。习惯了一种语言,可能就习惯了它的某些缺点,让你容易忽视背后的风险。
分享到:
评论

相关推荐

    Android代码-Console

    An Android console view, which allows you to log text using static calls, to easily debug your application, whilst avoiding memory leaks. Usage Include Console anywhere in your layout: then ...

    MemoryAnalyzer-1.7.0.20170613-macosx.cocoa.x86_64.zip

    The Eclipse Memory Analyzer is a fast and feature-rich Java heap analyzer that helps you find memory leaks and reduce memory consumption. Use the Memory Analyzer to analyze productive heap dumps with...

    Android-Memory-Leaks:这是一个示例,说明Android中的片段如何发生内存泄漏。 它也有解决问题的方法

    标题“Android-Memory-Leaks”直指这个问题的核心,即在Android应用程序中,特别是涉及到Fragment时,如何出现内存泄漏以及如何解决这些问题。Fragment是Android SDK中的一个关键组件,用于构建动态和模块化的用户...

    python_memory_leaks:调查 Python 内存泄漏的经验教训

    Python 内存泄漏注意事项工具pdb [链接] ( ) Heapy [链接] ( ) ObjGraph [链接] ( ) Memtop [链接] ( ) Muppy [链接] ( )赫鲁库记忆解读: 常驻内存(memory_rss):在 RAM 中保存的 dyno内存部分(兆字节)。...

    Android代码-AndroidKeyboardWatcher

    AndroidKeyboardWatcher Software keyboard open/close watcher for Android. ... To prevent memory leaks make sure to unbind it in onDestroy() method. public class MainActivity extends Activity implements K

    Expert.Android

    436 pages Publisher: Apress; 1 edition (July 3, 2013) Language: English ISBN-10: 1430249501 ISBN-13: 978-1430249504 What you’ll learn ...How to eliminate memory leaks and poor-performing code

    Android代码-loading button

    Progress Button Android Android Button that morphs into a loading progress bar. Fully customizable in the XML ...Avoid Memory Leaks Be Creative Bugs and feedback Credits Installation implement

    Android代码-katana

    Katana Katana is a lightweight, minimalistic dependency injection library (similar to the service ... Likelihood of memory leaks is greatly reduced unless YOU are doing something wrong ;P No reflecti

    Android代码-turbolinks-android

    Existing projects built with this version of the adapter will continue to work, although there may be some slow memory leaks and other minor issues. We will not be maintaining this adapter any ...

    Android代码-Android-Architecture-Components

    It can solve problems with configuration changes, supports data persistence, reduces boilerplate code, helps to prevent memory leaks and simplifies async data loading into your UI. I can’t say that ...

    android面试题.zip

    - Memory Leaks:内存泄漏的识别和解决方法。 - ANR(Application Not Responding)的原因和避免策略。 - 帧率(FPS)和渲染管道:如何提高UI流畅性。 - 资源优化:比如图片压缩、字符串本地化、选择合适的资源...

    CommonsWare.The.Busy.Coders.Guide.to.Android.Development.Version.8.2.2017

    Finding Memory Leaks Issues with System RAM Issues with Battery Life Power Measurement Options Sources of Power Drain Addressing Application Size Issues The Role of Scripting Languages The Scripting ...

    Android代码-RxLifecycle

    This capability is useful in Android, where incomplete subscriptions can cause memory leaks. Usage You must start with an Observable representing a lifecycle stream. Then you use RxLifecycle to bind a...

    The Busy Coders Guide to Android Development最终版2019

    Finding Memory Leaks Issues with System RAM Issues with Battery Life Power Measurement Options Sources of Power Drain Addressing Application Size Issues Crash Reporting Using ACRA In-App Diagnostics ...

    Android面试题

    - Memory Leaks和如何检测与避免,如使用MAT或LeakCanary工具。 - 使用Android Profiler进行性能分析,关注CPU、内存、网络和渲染性能。 - 对于UI流畅性的优化,如避免主线程阻塞,使用ViewStub和RecyclerView等...

    android内存管理-MAT与防范手段.pdf

    - **分析方法:** 利用MAT提供的多种视图工具,例如Memory Leaks Suspects Report等。 **3. 使用MAT比较Heap Dumps** - **应用场景:** 对比不同时间点的Heap Dump文件,观察内存使用变化趋势。 - **操作步骤:** ...

Global site tag (gtag.js) - Google Analytics