We have a large ReadOnlyDictionary
that serves as a high-speed cache for a large number of data objects we need to have quick access to.
On a timer, we regularly load up a replacement cache, then when it's completely loaded, we replace the existing cache object with the newly created one so that operations continue without interruption.
Recently, we're running into some memory leak issues, and one of the things we're examining is this cache replacement.
A simplified write-up of what we are doing:
public class Optimizations
{
private volatile ReadOnlyDictionary<string, Dictionary<string, object>> _cache;
public object GetFromLocalCache(string dataSet, string key)
{
object obj;
Dictionary<string, object> dict;
if (_cache.TryGetValue(dataSet, out dict) && dict.TryGetValue(key, out obj))
{
return obj;
}
return null;
}
private void OnTimerEvent()
{
ReadOnlyDictionary<string, Dictionary<string, object>> newCache = ...
_cache = newCache;
}
}
I'm wondering if the data that was previously in the _cache
is properly garbage collected, since we seem to be abandoning it for a new instance. Should we be doing something different to make sure the previous cache is properly destroyed, and the data is reclaimed by the OS?