Do not do anything wrong with Android to cause memory leakage

Source: Internet
Author: User

InAndroidContext can perform many operations, but the main function is to load and access resources. There are two kinds of context in android, one is application context and the other is activity context. Generally, activity context is transmitted between different types and methods, for example, onCreate of an activity.

Java code:

 
 
  1. Protected void onCreate (Bundle state ){
  2. Super. onCreate (state );
  3. TextView label = new TextView (this); // pass context to view control
  4. Label. setText ("Leaks are bad ");
  5. SetContentView (label );
  6. }

Passing the activity context to the view means that the view has a reference pointing to the activity and then referencing the resources occupied by the activity: view hierachy and resource.

In this way, if the context memory leaks, a lot of memory will be leaked.

This vulnerability indicates that gc cannot recycle activity memory.

Leaking an entire activity is an easy task.

When the screen rotates, the system destroys the current activity, saves the status information, and creates a new one.

For example, if we write an application that needs to load a large image, we do not want to destroy the image and reload it every time we rotate the screen. The simple idea of implementing this requirement is to define a static Drawable, so that the Activity class creation and destruction is always stored in the memory.

Implementation is similar:

Java code:

 
 
  1. public class myactivity extends Activity {   
  2. private static Drawable sBackground;   
  3. protected void onCreate(Bundle state) {   
  4. super.onCreate(state);   
  5. TextView label = new TextView(this);   
  6. label.setText("Leaks are bad");   
  7. if (sBackground == null) {   
  8. sBackground = getDrawable(R.drawable.large_bitmap);   
  9. }   
  10. label.setBackgroundDrawable(sBackground);//drawable attached to a view   
  11. setContentView(label);   
  12. }   
  13. }  

This program looks very simple, but it has a big problem. When the screen is rotated, there will be a leak, that is, the gc cannot destroy the activity ). As we said earlier, when the screen is rotated, the system will destroy the current activity. However, when drawable is associated with the view, drawable saves the view reference, that is, sBackground saves the label reference, while the label stores the reference of the activity. Since drawable cannot be destroyed and neither referenced nor indirectly referenced by it can be destroyed, the system cannot destroy the current activity, resulting in Memory leakage. Gc is powerless for this type of Memory leakage.

To avoid Memory leakage, you can avoid any activity whose lifecycle is longer than that of the activity, and prevent the activity from being destroyed due to its reference to the activity. We can use application context. Application context is associated with the life cycle of the application and has nothing to do with the life cycle of the activity.Application contextYou can useContext. getApplicationContextOrActivity. getApplicationMethod.

To avoid Memory leakage related to context, remember the following points:

1. Do not make an object with a long life cycle reference the activity context. That is, ensure that the object that references the activity has the same life cycle as the activity itself.

2. For objects with a long life cycle, you can use application context.

3. Avoid non-static internal classes and try to use static classes to avoid lifecycle issues. Pay attention to the lifecycle changes caused by internal class references to external objects.

Insert a new Activity in Android Development

Android development: Passing values between activities

Android Activity and Intent mechanism learning notes

Android development: Custom GridView/ListView Data Source

Related Article

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.