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

Clear cache ? #24

Closed
lalith-b opened this issue Sep 26, 2013 · 16 comments
Closed

Clear cache ? #24

lalith-b opened this issue Sep 26, 2013 · 16 comments

Comments

@lalith-b
Copy link

The application cache keeps growing past the 50mb mark for diskcache, should we write a logic ourselfs to clear the cache in the onTrimMemory() ?

@sjudd
Copy link
Collaborator

sjudd commented Sep 26, 2013

Do you mean the size on the sd card of the disk cache grows past 50mb? Or the size of the memory cache grows past 50mb?

The limit you set for the disk cache is not necessarily a hard limit, according to the docs for the disk cache (https://github.com/JakeWharton/DiskLruCache), it may go over that limit by a small amount occasionally. If the disk cache is getting significantly larger than that limit, that's probably a bug and I can look in to it.

@lalith-b
Copy link
Author

The size grew up to 64MB last time I checked. And the cache always stayed in 60+MB on extensive usage of the app. We could clear the cache in onTrimMemory(). I wrote code to clear the directory but it crashes as the DiskLruCache holds references to the disk for images. Also skip cache would be possible if we are just able to find the reference to the disk image and delete them.

Guess it's a good idea to give apes to clear the disk cache. Please find my implementation for clearing cache onTrimMemory ().

public static void clearCache(Context context){
    File cacheDir = ImageManager.getPhotoCacheDir(context, "cache_dir");
    if (cacheDir.isDirectory()){
        for (File child : cacheDir.listFiles()){
            child.delete();
        }
    }
}

@csobrinho
Copy link
Contributor

+1

@sjudd
Copy link
Collaborator

sjudd commented Oct 10, 2013

Sorry I'm still a little unclear on what you're looking for. Do you want to be able to clear out the memory cache onLowMemory? Or the disk cache when the sd card is getting full? Or both?

If you'd simply like the disk cache to use less space you can always set a lower limit. As long as it only exceeds the limit by a certain amount. I know that's not ideal, but it's also non trivial to write a disk cache.

@csobrinho
Copy link
Contributor

For me, the memory cache but a disc cache clean can also be go as a maintenance task.

@sjudd
Copy link
Collaborator

sjudd commented Oct 15, 2013

I'm a little hesitant to add this for either the memory or the disk cache, but I could be talked in to it.

For the memory cache: You should be able to choose a safe memory cache size either for all devices or per device (as we do by default: https://github.com/bumptech/glide/blob/master/library/src/com/bumptech/glide/resize/ImageManager.java#L96) and never need to clear the cache. If there are memory leaks in either Glide or in applications using Glide, those should be fixed separately.

For the disk cache: Assuming the size is bounded, even if the actual size on the sd card is somewhat larger than the max size you set, you can always decrease the max size until the actual size matches your target size. Clearing the catch will cause some large performance problems and also doesn't really solve the problem since the cache will just increase in size again until you happen to clear it.

@savvasdalkitsis
Copy link
Contributor

What we did (since we need to clear the cache too), is to create our own LruMemoryCache (based on the one from Glide) and expose two methods to clear and trim the cache. We then made Glide use our own instance which we expose to other parts of the app which handle the trimming and clearing

@csobrinho
Copy link
Contributor

Nice. Also a good alternative.
I have a similar approach in the disk cache of Volley to set the expiration manually to one week.

@sjudd
Copy link
Collaborator

sjudd commented Nov 5, 2013

Savvas if you want to throw up a pull request with that cache, I'd be happy to take a look and try to expose those methods through Glide (clearMemoryCache and clearDiskCache ?).

I'll bow to the majority opinion here if you all would find it useful.

@savvasdalkitsis
Copy link
Contributor

Will do as soon as I make some needed changes in our fork.

@savvasdalkitsis
Copy link
Contributor

I submitted pull request:

#35

One note: I am not sure if I should be notifying the listener of removed bitmaps when clearing the cache completely. Any thoughts?

@sjudd
Copy link
Collaborator

sjudd commented Nov 10, 2013

@csobrinho I'd also be curious to see what you did with Volley to expire the cache. If Volley is only used for Glide, it might almost make sense to disable Volley's disk cache entirely since we do a better job of caching the minimal amount of data in Glide in most circumstances.

@lalith-b
Copy link
Author

GC goes haywire trying to clear and maintain 60% free space alltime after the pull.

@sjudd
Copy link
Collaborator

sjudd commented Nov 13, 2013

Are you calling the methods on the cache yourself? By default Glide doesn't actually call either clearMemory or trimMemory. If you have and are seeing issues I'll look in to it.

@lalith-b
Copy link
Author

yes without calling the clearMemory/trimMemory the GC goes wild, i suppose there are some memory leaks.

@sjudd
Copy link
Collaborator

sjudd commented Nov 24, 2013

Add clearMemory method here: 6244968

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

4 participants