`
Teok
  • 浏览: 147806 次
  • 性别: 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 ...

    Efficient Android Threading(O'Reilly,2014)

    Use strategies to reduce the risk of memory leaks Manage the lifecycle of a basic thread Run tasks sequentially in the background with HandlerThread Use Java’s Executor Framework to ...

    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...

    Apress.Expert.Android.2013

    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.High.Performance.Programming.1785288954

    This book explains techniques for building optimized and efficient systems that do not drain the battery, cause memory leaks, or slow down with time. Style and approach The book follows a tutorial-...

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

    Android内存泄漏这是一个示例,说明Android中的片段如何发生内存泄漏。 它也有解决该问题的方法。 有关此问题的深入说明,请参见本文

    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 ...

    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应用的性能问题

    本文作者就解决Android应用性能问题给出了三个方面,即过度绘制、Android图形渲染以及MemoryLeaks内存泄漏。无论你的应用多么有创新性、有用,如果它卡得要命,或者非常消耗内存,那么每人将会愿意使用它。因此,...

    Boost C++ Application Development Cookbook 1st 原版pdf by Polukhin

    libraries that are now mostly part of C++11 and leave no chance for memory leaks. Managing resources will become a piece of cake. We'll see what kind of work can be done at compile time and what Boost...

    EurekaLog_7.5.0.0_Enterprise

    7)....Added: Streaming unpacked debug info into temporal files instead of memory - this greatly reduces run-time application memory usage at cost of slightly slower exception processing. This also ...

Global site tag (gtag.js) - Google Analytics