I have a .NET2.0 C# web-app. It has a variable number of large, init-expensive objects which are shared across multiple requests but not sessioned to any given user. I therefore need to persist them in a lookup structure. These objects need to be created as required and are not required for the lifespan of the app, merely the lifespan of their usage.
Plus a bit.
The memory leaking way to do this is a simple dictionary, the memory safe way to do this is a weakreference backed dictionary, but the problem I'm having is that the GC is just too damned fast. Realistically this may not be a problem because the traffic to the objects should be such that they'll stay alive without being forced to regenerate too much, but ideally I'd like them to scale down too.
Is there some kind of middle-ground solution I'm not thinking of which will keep the objects safely hidden from the GC for a period of time X, but also allow them to be collected at the end of that time, preferably where that time counter gets reset every time they're used in a way similar to session tokens?