Add a secondary weak memory cache to improve speed and decrease memory usage when reusing previous images #791
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hey guys! This addition to the project can improve speed and significantly reduce memory usage in cases where the regular memory cache has been cleared but images it once held need to be reloaded.
Consider the following case:
With this addition, all downloaded images are also referenced by the secondary weak memory cache (so they are not retained) and reused when possible to prevent a single image from ever being duplicated by SDWebImage.
Sample
To see the fix in action, you can download this sample project: http://cl.ly/2J3s0z2e2m2C
Move the comment in the podfile to switch between my fixed fork and the current latest commit in master.
Open the app and trigger the case by pushing a new view and then triggering a memory warning. Repeat this and watch the effects in Instruments: The allocations for
UIImage
continue to rise (In the Allocations instrument) and the Virtual Memory usage (in the Memory Monitor instruments) rises by several mb with each additional load. Switch to my fork and you will notice the UIImage only ever gets created once no matter what you do, and memory usage remains relatively static (rising very slowly due to multiple views and view controllers being allocated).