Setting sdwebimage in maxcachesize dynamic management picture memory cache size is invalid. Definitions and use of maxcachesize and Maxmemorycost.

Source: Internet
Author: User

After fixing a strong reference as described in my previous blog post, Sdwebimage cannot release its own image cache, although it can already erase the memory cache when it receives a memory alert, but still occasionally encounters a flashback. I guess it may be because the memory alarm when the image is being downloaded the size of the larger, causing the memory alarm can not be processed, the memory is blown. So consider using Maxmemorycost or maxcachesize to control the size of the memory cache. When you do not set Maxmemorycost, but only set the maxcachesize, the memory usage in leaks is still increasing. did not achieve the desired effect

Read official documents and various online articles to find out whether the specific definition of maxcachesize is first confused. Finally, the maxcachesize found in issue 1206 of Sdwebimage in GitHub is only used in the Cleandiskwithcompletionblock method, and this method is only Cleandisk ( Manual cleanup of the disk cache) and Backgroundcleandisk (cleaning the disk cache when the app is cut into the background) does not dynamically maintain the cache size. Therefore, the memory size is not dynamically managed while the program is running.

The Maxmemorycost is the Totalcostlimit property that sets the Nscache directly, and Nscache dynamically keeps the memory cache size. Also maxmemorycost sets the total number of pixels in the memory cache for all photos.

Maxcachesize means the maximum size that can be stored in the disk cache when the disk cache is cleared. The memory cache size is not dynamically controlled regardless of the memory cache.

The code snippet used in the Cleandiskwithcompletionblock method to Maxcachesize is as follows:

        If Our remaining disk cache exceeds a configured maximum size, perform a second//size-based cleanup P  The.
        We Delete the oldest files first. if (self.maxcachesize > 0 && currentcachesize > Self.maxcachesize) {//Target half of our Maxim
            Um cache size for this cleanup pass.

            Const Nsuinteger desiredcachesize = SELF.MAXCACHESIZE/2;
            Sort the remaining cache files by their last modification time (oldest first).
                                                            Nsarray *sortedfiles = [Cachefiles keyssortedbyvaluewithoptions:nssortconcurrent
                                                                Usingcomparator:^nscomparisonresult (ID obj1, id obj2) {
                                                            return [Obj1[nsurlcontentmodificationdatekey] compare:obj2[nsurlcontentmodificationdatekey]];

            }];
 Delete files until we fall below our desired cache size.           For (Nsurl *fileurl in sortedfiles) {if ([_filemanager removeitematurl:fileurl Error:nil]) {
                    Nsdictionary *resourcevalues = Cachefiles[fileurl];
                    NSNumber *totalallocatedsize = Resourcevalues[nsurltotalfileallocatedsizekey];

                    Currentcachesize-= [Totalallocatedsize unsignedintegervalue];
                    if (Currentcachesize < desiredcachesize) {break;
 }
                }
            }



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.