Problems with Android loading pictures with LRUCache

Source: Internet
Author: User

When a ListView is used to load a large image, a memory overflow is encountered and crashes.

But the use of LRUCache was supposed to solve the problem.   However, there is a memory overflow condition found. Troubled for a long time

In addition, after the picture has been downloaded and saved locally, it is found that when a large number of images are read, the use of LRUCache does not cause a crash due to memory leaks.

Finally through the view log found that in the network loading time to eat memory, with the continuous generation of threads to download pictures, can see the virtual machine to free up memory when the basic function of releasing up to more than 10 K per time, memory is almost full ...

While loading the local image, memory release is indeed quite timely, about 4M each time, so of course there will be no memory leaks ... Where does the problem occur?

Consider the possible factors: 1 Too many threads open.

But loading the local picture is also the opening of a new thread ... But it didn't crash.

2 Downloads too many temporary bitmap that have failed to release the cause?

But after the network loaded successfully I also put the download bitmap in the LRUCache, ah, supposedly he should be in excess of the given laughter to help me free memory ah ... Why is it? Is there a place where there is a blockage, and this place is constantly stacking bytes into memory? So I found the place to download the pictures.

Conn.connect ();
Bitmap = Bitmapfactory.decodestream (is,null,bmpfactoryoptions);

The problem should be here, when too many threads are being opened to download images, the download process buffers too many byte[] and waits for the data to come in their own threads. In this way, the virtual machine does not release this data, and multiple threads are blocked, causing a memory overflow. And that leads to the problem.

To avoid this problem, there are two solution ideas, 1 less open a few threads 2 pictures not too big.

So I don't know if it's right .... Yes, I think so.

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.