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?
I’m not sure why the HttpRuntime cache would not work here. Items inserted into the cache will be ‘renewed’ every time they are touched, thus keeping them alive until they are no longer needed; and after that they will continue to stay in cache until they expire (sliding or rolling time) or they are forced out due to memory pressure. They can also be forced out at an explicitly-set, absolute time regardless of usage:
Absolute time: items are forced out after the system clock passes a certain DateTime
Sliding (rolling) time: every time an item is touched, its countdown to death is reset. The duration of the countdown is the sliding time (for example, 5 minutes).
Example usage: