Andoid Memory leakage and andoid Leakage

Source: Internet
Author: User

Andoid Memory leakage and andoid Leakage

1. android Memory leakage occurs because the allocated memory is recycled.

Public static LocalHelper getInstance (Context context ){
If (instance = null ){
Instance = new LocalHelper (context );
}
Return instance;
}

If you upload an Activity in contenx, the activity cannot be recycled after Acitivty is finished.
Because the static instance will continue to reference the activity, memory GC will not cause oom and Memory leakage.

2. Memory leakage caused by Handler

Memory leakage caused by Handler usage should be said to be the most common

We know that Handler, Message, and MessageQueue are all associated with each other.

If the Message sent by Handler is not processed, the Message and the Handler object sent by Handler will be held by the thread MessageQueue.

Because Handler is a TLS (Thread Local Storage) variable, the lifecycle and Activity are inconsistent.

Therefore, this implementation method is generally difficult to ensure that it is consistent with the life cycle of the View or Activity, so it is easy to cause the release to fail correctly.

We recommend that you use weak references, but it may be difficult to write activity. member variables!

3. When imageloader loads an image, if you use Rgb_565, you can reduce the memory consumption when loading the image. However, the uploaded image will be blurred. We recommend that you use imagesize,

options = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(false)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
ImageSize size = new ImageSize(getImageWidth(), getImageWidth());
ImageLoader. loadImage (localFile. getOriginalUri (), size, options, new SimpleImageLoadingListener (viewHolder. imageView ));
When imageload loads the source image, if the image is too large, we recommend that you use the number of threads as 1 because it is easy to oom when three threads are simultaneously loaded;

4. Remember to clear () the static collection class object ();

 

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.