Implementing a memory management solution without OOM on android

Source: Internet
Author: User

 

Many people complain that a large number of images are loaded in the listview on android, and other pages are loaded with larger images than those in the listview. A large number of different types of images are combined, and the user experience must be taken into account, at this time, applications often use OOM, which is hard to worry about. Below are two methods that can be tested with strong strength for your reference. Correct me:

 

Solution 1: simple linked list + soft Reference Management

1. Define a single-instance class (e.g. EstoreCacheManager) and initialize it as a hungry man;

 

2. Define a LinkedHashMap and soft reference to bind the linked list and soft reference:

(

Private SoftReference mImageReference;

Private LinkedHashMap mImageMap;

 

MImageMap = new LinkedHashMap ();

MImageReference = new SoftReference (mImageMap );

}

 

3. When performing the insert operation, determine whether the size of LinkedHashMap exceeds a set value (e.g.: 200), delete the oldest one, and then insert it;

 

4. Capture exceptions when downloading images:

{

Try {

MBitmap = loadImageByUrl (url );

} Catch (OutOfMemoryError e ){

// The VM does not always free-up memory as it shocould,

// So manually invoke the garbage collector

// And try loading the image again.

System. gc ();

MBitmap = loadImageByUrl (url );

}

}

 

Solution 2: complex LRU Method management (implemented by modifying and optimizing an open-source Library)

For more information, see the demo project and source code I wrote for a new one:

Http://code.google.com/p/androidforyouku

 

From androidzhaoxiaogang's column

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.