public static void Main(string[] args) { #pragma warning disable 219 int size = 20; MyCache cache = new MyCache(size); mco = new MyCacheableObject("0"); cache.addEntry("0", mco); for (int i = 1; i < 5; i++) { MyCacheableObject temp = new MyCacheableObject(i.ToString()); cache.addEntry(i.ToString(), temp); temp = null; //csc and dotnet require this in order to work correctly, dmcs does not } sleepAndPrintCount(cache); GC.Collect(); //All but one entry will be finalized MyCacheableObject mco2 = cache.getEntry("3"); //Example of getting object before finalize sleepAndPrintCount(cache); Console.WriteLine("One entry reachable from root set, another entry gotten after gc before finalize so it is not tracked."); mco2 = null; GC.Collect(); sleepAndPrintCount(cache); Console.WriteLine("One entry reachable from root set, second chance added to lru tracking"); mco = null; GC.Collect(); sleepAndPrintCount(cache); Console.WriteLine("All entries should now be lru tracked"); #pragma warning restore 219 }
public static void sleepAndPrintCount(MyCache cache) { System.Threading.Thread.Sleep(1000); //Make time for finalize to run Console.WriteLine("Count of elements: " + cache.LRUCount()); }
public void setCache(MyCache cache) { //_cache = new WeakReference(cache); _cache = cache; }