Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image cache behavior question #3698

Open
willbattel opened this issue Apr 11, 2024 · 2 comments
Open

Image cache behavior question #3698

willbattel opened this issue Apr 11, 2024 · 2 comments

Comments

@willbattel
Copy link

willbattel commented Apr 11, 2024

The API headers show that there is no limit for the size of the memory and disk caches by default.

* Defaults to 0. Which means there is no memory cost limit.

What happens if the cache continues to grow until the device runs out of memory / disk space? Does SDWebImage alter its caching behavior based on the amount of available memory and disk space? Surely it would be problematic if, over the lifecycle of an app session, the cache consumed more and more memory until there was no memory left for anything else.

What would happen if the device runs out of disk space? Would SDWebImage try to continue caching things and fail, or would it reduce the size of the disk cache?

Currently we are manually checking the amount of disk space and physical memory of the device to set the cache limits, but we want to know what would happen with the default behavior.

Thank you.

@dreampiggy
Copy link
Contributor

dreampiggy commented Apr 12, 2024

What happens if the cache continues to grow until the device runs out of memory / disk space

NSCache has its own policy to remove some items

Cache objects differ from other mutable collections in a few ways:
The NSCache class incorporates various auto-eviction policies, which ensure that a cache doesn’t use too much of the system’s memory. If memory is needed by other applications, these policies remove some items from the cache, minimizing its memory footprint.
You can add, remove, and query items in the cache from different threads without having to lock the cache yourself.
Unlike an NSMutableDictionary object, a cache does not copy the key objects that are put into it.

And then, iOS system will send UIApplicationDidReceiveMemoryWarning notification.

Does SDWebImage alter its caching behavior based on the amount of available memory and disk space

We currently do nothing. But can do.

The cache impl use NSCache, from Apple...So we can provide a KVO based observation on that NSCache and notify to you (via a callback or notification ?)

@willbattel
Copy link
Author

Thank you for the info. Is there a recommended practice for setting the maximum cache size for each device, or would it be simplest to set a fixed size that should accommodate effectively all devices and leave it at that? For example, 50 megabytes for memory cache and 100 megabytes for disk cache.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants