Android Memory Optimization

Source: Internet
Author: User

Private Static drawable sbackground;

@ Override

Protected void oncreate (bundle state ){

Super. oncreate (State );

Textview label = new textview (this );

Label. settext ("leaks are bad ");

If (sbackground = NULL ){

Sbackground = getdrawable (R. drawable. large_bitmap );

}

Label. setbackgrounddrawable (sbackground );

Setcontentview (Label );

}

Label. setbackgrounddrawable (sbackground); this sentence uses label as the callback of sbackground. Since drawable needs to call label, it will have reference of label.

Textview label = new textview (this); the context is obviously passed to the label as a parameter, so the label has a reference to the context.

If the sbackground cannot be GC, the label cannot be GC, and the context cannot be GC. The resources that reference the context cannot be GC, causing a large memory leakage.

Non-static internal class accesses to external classes are also prone to memory leakage. We recommend that you use static classes and use weakreference to access external classes.

Difference between weakreference and softreference:

The biggest difference is that weakreference will be released immediately (not referenced), and softreference will be released at the latest (with the risk of memory overflow.

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.