I'm developing an Android-based 3D game in Java (not using the NDK). One of the must-haves for any game is a resource cache, in which I can store loaded textures, meshes, sounds, etc.
My plan is to develop a resource cache that hands out weak references to certain resources, so that only the cache itself keeps a strong reference to the resource, meaning that if I remove a resource from my cache, it will be garbage collected without me having to worry about it.
I'm having a hard time finding an answer to the following question though: Say I have filled my resource cache to the point where my heap space is almost completely full. I now want to load an additional resource, but its memory footprint is so large that it won't fit in my available heap space. To accommodate for the new object's size, I remove some entries from my cache and I load in the new object. Will this possibly cause an OutOfMemory exception? As long as the garbage collector hasn't sweeped through my recently removed objects, the memory stays in use, so is the garbage collector smart enough to run a new sweep in order to accommodate the newly allocated object?