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;
}
}
}