Android memory leaks

Source: Internet
Author: User

Original:http://android-developers.blogspot.jp/2009/01/avoiding-memory-leaks.html


The stack size of the Andriod app is limited to 16MB on the T-mobile G1, which is already a lot of memory for a phone, but not enough for some developers. Even if you don't need to use so much memory space, you should try to save as much as possible so that you don't kill yourself while other apps are running. The more apps Android can cache in-memory, the smoother the switch between apps will be. As part of my work, I learned more about the memory leaks in Android applications and found that most of them are in the same class--long time to keep a reference to a context (keep a long_lived reference to a context).

In Android, many operations need to use context, but most of the situation is to get and load resources, which is why all the controls need to receive a context parameter in the constructor. In general, we can get two kinds of context,activity and application, and developers will usually pass the first activity as a parameter.

@Overrideprotected void OnCreate (Bundle state) {  super.oncreate (state);    TextView label = new TextView (this);  Label.settext ("Leaks is Bad");    Setcontentview (label);}

This means that view holds a reference to the entire activity and everything inside him, such as the entire view hierarchy and all the resources. So if you leak the context (the "leak" means that you keep his references so that the GC can't be recycled in time), you leak a large chunk of memory. It's easy to spill the whole activity if you're not careful.

When the screen orientation changes, the system defaults to destroy the current activity, saves the current state and creates a new activity, that is, Android will reload the app's view from the resource file. Imagine that there is a big picture in your app, but it's not like every time you rotate the screen to reload it, the simplest way to cache it in memory is to declare it as a static type:

private static drawable sbackground; @Overrideprotected void OnCreate (Bundle state) {  super.oncreate (state);    TextView label = new TextView (this);  Label.settext ("Leaks is Bad");    if (Sbackground = = null) {    Sbackground = getdrawable (R.drawable.large_bitmap);  }  Label.setbackgrounddrawable (sbackground);    Setcontentview (label);}

The code above runs fast, but it's wrong, and when the screen spins, it leaks the entire activity, when a drawable is linked to the view, This view is set to a callback of drawable, in the above code, which means that drawable maintains a textview reference, and TextView maintains a reference to the entire activity, It's almost related to everything in the activity.

This example is the simplest case of a context leak, you can see in the source code of Homescreen How we solve this problem by setting the drawable callback to null (the search Unbinddrawables () method), Interestingly, sometimes a call chain that leaks the context is created, which can make your memory run out faster.
There are two simple ways to avoid context-related memory leaks, one to avoid the context escaping his own range (avoid escaping the context outside of its own scope), The example above shows a situation where a static reference causes a leak, but the internal class's strong references to the external classes are equally dangerous. Another workaround is to use the application context, which will persist until your app exits without relying on the activity's lifecycle. If you have a long-lived object, and the object has a context reference, remember to use application context, which you can easily pass through Context.getapplicationcontext () or activity.getapplication () to get.
In short, avoid context-related memory leaks, remember the following several
    • Do not keep a reference to the activity context type for a long time (a reference to the activity should have the same life cycle as the activity)
    • Use application context instead of activity context
    • Avoid using a non-static (non-static) inner class in an activity, if you don't care about his life cycle, use a static inner class, and then maintain a weak reference to activity within the class, as in the W inner class in the Viewroot class.
    • Garbage collection does not guarantee memory leaks

Android memory leaks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.