`
iwebcode
  • 浏览: 2004377 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

Android应用程序避免Context相关的内存泄露的方法

 
阅读更多

There are two easy ways to avoid context-related memory leaks.

1).The most obvious one is to avoid escaping the context outside of its own scope.

2).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() orActivity.getApplication().


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

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

2).Try using the context-application instead of a context-activity

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

4).A garbage collector is not an insurance against memory leaks.


详情请见:http://developer.android.com/resources/articles/avoiding-memory-leaks.html

分享到:
评论

相关推荐

    Android webview 内存泄露的解决方法

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

    避免 Android中Context引起的内存泄露

    本文主要介绍Android中Context引起的内存泄露的问题,这里对Context的知识做了详细讲解,说明如何避免内存泄漏的问题,有兴趣的小伙伴可以参考下

    谈谈Android里的Context的使用

    大家好,今天给大家分享一下Android里的Context的一些用法. 这里大致可以分为两种:一是传递Context参数,二是调用全局的Context. 其实我们应用启动的时候会启动Application这个类,这个类是在AndroidManifest.xml...

    android检查内存泄露

    android检查内存泄露和用法 <!--==============================================================================--> android:name="com.squareup.leakcanary.internal.HeapAnalyzerService" android:enabled...

    Android全局变量和Context

    Android全局变量和Context的实现方法

    Android应用程序中读写txt文本文件的基本方法讲解

    主要介绍了Android应用程序中读写txt文本文件的基本方法讲解,基本上依靠context.openFileInput()和context.openFileOutput()两个方法为主,需要的朋友可以参考下

    android demo,BaseAdapter传递context的应用

    android demo,BaseAdapter传递context的应用,

    Android编程中避免内存泄露的方法总结

    作为我的一项工作,我仔细研究了Android应用的内存泄露问题,大多数情况下它们是由同一个错误引起的,那就是对一个上下文(Context)保持了长时间的引用。 在Android中,上下文(Context)被用作很多操作中,但是大...

    5个Android开发中比较常见的内存泄漏问题及解决办法

    了解了内存泄漏的原因及影响后,我们需要做的就是掌握常见的内存泄漏,并在以后的Android程序开发中,尽量避免它。下面搜罗了5个Android开发中比较常见的内存泄漏问题及解决办法,分享给大家,一起来看看吧。  一、...

    android中用getApplicationContext()会不会避免某些内存泄漏问题?

    RT,在android开发中,如果在使用context的地方全部用getApplicationContext()会不会避免某些内存泄漏问题? 首先,Activity的Context和Application的Context肯定不是一个东西,一个是当前活动的 Context,它的生命...

    Android编程获取全局Context的方法

    主要介绍了Android编程获取全局Context的方法,实例分析了基于Application类获取全局Context的实现步骤与相关技巧,需要的朋友可以参考下

    Android Context使用例子.

    Android Context使用例子.

    Android应用内悬浮窗的实现方案示例

    悬浮窗,大家应该也不陌生,凌驾于应用之上的一个小弹窗,实现上很简单,就是添加一个系统级别的窗口,Android中通过WindowManagerService( WMS)来管理所有的窗口,对于WMS来说,管你是Activity、Toast、Dialog,...

    Android实现获取应用程序相关信息列表的方法

    本文所述为Androdi获取手机应用列表的方法,比如获取到Android应用的软件属性、大小和应用程序路径、应用名称等,获取所有已安装的Android应用列表,包括那些卸载了的,但没有清除数据的应用程序,同时在获取到应用...

    ANDROID 应用完全退出

    Android怎样才能完全退出程序? 本Android程序解决了完全退出,引用context未释放引起的oom等问题。

    Android Context 浅析

    简要介绍android context 的用法

    Android编程实现全局获取Context及使用Intent传递对象的方法详解

    本文实例讲述了Android编程实现全局获取Context及使用Intent传递对象的方法。分享给大家供大家参考,具体如下: 一、全局获取 Context Android 开发中很多地方需要用到 Context,比如弹出 Toast、启动活动、发送广播...

    Android常见的几种内存泄漏小结

    一、背景 ...由于Android系统为每个应用程序分配的内存有限,当一个应用中产生的内存泄漏比较多时,就难免会导致应用所需要的内存超过这个系统分配的内存限额,这就造成了内存溢出而导致应用Crash。了

Global site tag (gtag.js) - Google Analytics