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)