I’m developing some cross-platform software which is intended to run on mobile devices. Both iOS and Android provide low memory warnings. I plan to make a wrapper class that will free cached resources (like textures) when low memory warnings are issued (assuming the resource is not in use). If the resource returns to use, it’ll re-cache it, etc… I’m trying to think of what this is called. In .Net, it’s similar to a “weak reference” but that only really makes sense when dealing with garbage collection, and since I’m using c++ and shared_ptr, a weak reference already has a meaning which is distinct from the one I’m thinking of. There’s also the difference that this class will be able to rebuild the cache when needed. What is this pattern/whatever is called?
Edit: Feel free to recommend tags for this question.
5
One term used for this is “Scavenger”, though I’ve mostly seen it in the context of Microsoft technologies. See here in the ASP.NET Caching Overview page:
Scavenging
Scavenging is the process of deleting items from the cache when memory is scarce. Items are removed when they have not been accessed in some time or when items are marked as low priority when they are added to the cache.
And here’s a definition from Microsoft’s Caching in WinINet page:
The cache scavenger periodically cleans items from the cache. If an item is added to the cache and the cache is full, the item is added to the cache and the cache scavenger is scheduled.
So if you have an object whose task it is to free cached items when resources are low, it would be a Scavenger. You might have several scavenging strategies – Biggest First, Earliest First, etc., each implemented by a different Scavenger.
1
It is called a … Cache. :-). Seriously, intelligently flushing it is just as important to a good cache implementation than intelligently filling it.
But your example is not really a cache, since it is not optional. A texture MUST be loaded before it can be rendered. (I would use the terms load and unload in this case).
So I imagine a class:
- That is asked to make sure a texture image is loaded, every time it is used.
- That loads the image from the correct storage (eg. A certain resource folder)
- To keep track of texture usage patterns.
- which can be asked to unload textures, based on these usage patterns.
Then that would be called a TextureLoader to me (short for TextureLoaderUnloader)