Until now, I thought that the following construction would work with the GC:
public class SampleClass
{
#region Statics
public static int CacheCapacity
{
get => CacheCapacity_;
set
{
int val = value < 0 ? 0 : value;
if(ActiveSamples_ != null)
{
if(val == 0)
{
ClearCache();
CacheCapacity_ = 0;
return;
}
if (val < ActiveSamples_.Count)
{
ShrinkCacheTo((int)(0.8D * val));
}
}
CacheCapacity_ = val;
}
}
static int CacheCapacity_;
protected static List<SampleClass> ActiveSamples
{
get
{
if(ActiveSamples_ == null)
ActiveSamples_ = new List<SampleClass>();
return ActiveSamples;
}
}
protected static List<SampleClass> ActiveSamples_;
public static bool AddToCache(SampleClass instance)
{
if(CacheCapacity == 0)
return false;
if (ActiveSamples.Count >= CacheCapacity)
ShrinkCacheTo((int)(0.8D * CacheCapacity));
ActiveSamples.Add(instance);
return true;
}
public static void ClearCache()
{
ActiveSamples_ = null;
}
protected static void ShrinkCacheTo(int size)
{
if (ActiveSamples_ == null)
return;
if (ActiveSamples_.Count <= size)
return;
List<SampleClass> tmp = new List<SampleClass>(size);
for (int i = ActiveSamples_.Count-size; i < ActiveSamples_.Count; i++)
{
tmp.Add(ActiveSamples_[i]);
}
ActiveSamples_ = tmp;
}
#endregion
#region Constructors and Instance getters
public SampleClass(string instanceID)
{
InstanceID = instanceID;
// time-consuming initialisation ...
AddToCache(this);
}
public static SampleClass GetOrCreateInstance(string instanceID)
{
var result = ActiveSamples.Find(x => x?.InstanceID == instanceID);
if (result == null)
result = new SampleClass(instanceID);
return result;
}
#endregion
#region Instance members
public string InstanceID { get; protected set; }
public bool IsCached => ActiveSamples.Find(x => x?.InstanceID == InstanceID) == this;
#endregion
}
I allow to cache instances of SampleClass
(that takes quite long to initialise) up to a certain capacity so that I do not always create them when I reference them again.
As ActiveSamples_
is static, the cached instances will not be collected by the GC, which is intended as long as they are referenced by ActiveSamples_
. I supposed that they become garbage as soon as I assign null or a new List not containing them to ActiveSamples_
in ClearCache()
, ShrinkCacheTo()
(and, of course, if there are no further references to them).
Now my colleage who is hunting memory leaks read here (point 3 and 5) that my caching may raise the problem that, if the instances were once referenced by a static object, they will never become managed code afterwards. The code samples in the link do not provide any code that cuts the reference between the static List or Dictionary and its contents, so that it may only say that they will remain as long as they are referenced but the text can be understood in the way that the become and remain unmanaged.
My question is, what is correct:
- Former members of a static field are permanently out of the scope of the GC; they have to be disposed.
- Once the reference from the static field to the object is no more valid, the GC will automatically dispose them when it decides to do so.
If 1. is true how do I dispose them?