Resolution of memory leaks after loading large numbers of images with Sdwebimage reprint

Source: Internet
Author: User

Workaround time for memory leaks after loading large numbers of pictures with Sdwebimage:2015-07-21 14:26:47 read:5885 Comments:0 Favorites:0 [Point I collection +]

Sdwebimage's popularity is needless to say, on GitHub near 10k star, too many apps at home and abroad use it for picture loading.

But recently in the use of the process found that in the UITableView load more content, the use of sdwebimage will cause memory consumption is more and more large, resulting in memories Warning final terminate, a little to find the cause of the problem, Found that many developers have encountered this problem, the Chinese data did not search the solution to the problem, in order to facilitate the other developers in the country to meet similar problems do not waste time, this blog The solution is recorded as follows:

First check the processing of memory warning in the Sdwebimage code:

-(void) clearmemory {    [Self.memcache removeallobjects];}

Where Self.memcache is the Nscache type, you can see that the sdwebimage itself has performed an operation on the memory warning, but there is nothing X to use.

Using instruments's allocations to analyze memory usage:

You can see that the memory is basically in Decodedimagewithimage: This method is occupied, the method to see the internal implementation, the feeling is relatively normal, had to help Google

And finally found this piece of issue:https://github.com/rs/sdwebimage/issues/538 on GitHub.

The following reply a lot (it seems that many people have encountered this problem), even the author of Sdwebimage, RS also made a lot of replies below (although his reply does not have any x use), the reply said the problem causes and solutions are summarized as follows:

1.rs I reply: Sdwebimage used by the Nscache will be at the right time (memory warning) to free the RAM, many applications in loading a large number of images when the situation did not appear;

2. This problem is due to the sdwebimage of GIF support code, remove the relevant code;

The realization of 3.decodedImageWithImage directly retrun image can be;

4.https://twitter.com/0xced/status/332252283758845953, this Twitter is also pretty humorous, Popular translation is the solution described in this Twitter to save those programmers who are sdwebimage cause problems .

Try each of the above 4 solutions:

The first kind of nothing to say, before the analysis of the source time has been seen, that is, the solution of the RS is useless.

The second way, read the source can be found in the Sdwebimage GIF processing only for GIF pictures, does not affect the PNG or other format of the picture, so this method I feel useless, did not try.

The third kind, directly causes the memory occupation reason from the CG raster Data to become imageio_png_data, is also useless.

Fourth, I called this code at the start of the receive memory warning.

            [[Sdimagecache Sharedimagecache] Setvalue:nil forkey:@ "MemCache"];

Found that there will still be problems, and then try to load more content every time to execute once, and finally memory no longer continue to increase, that is, the fourth is the final solution, in the subsequent blog I may specifically analyze the cause of this problem and solve the principle.

Summarized as follows:

When you use Sdwebimage to load more pictures causing a memory warning, periodically call

[[Sdimagecache Sharedimagecache] Setvalue:nil forkey:@ "MemCache"];

can be resolved.

Resolution of memory leaks after loading large numbers of images with Sdwebimage reprint

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.