I thought the way the MemoryCache class worked was it held whatever you placed in it in memory as long as the CachePolicy was still met, and as long as memory conditions allow.
After running some tests I'm not sure MemoryCache takes any action when memory conditions are low.
I can run the following code in a console app:
MemoryCache cache = MemoryCache.Default;
CacheItemPolicy p = new CacheItemPolicy()
{
AbsoluteExpiration = ObjectCache.InfiniteAbsoluteExpiration
};
for (int i=0; i < 1000000; i++)
{
Console.WriteLine(i);
string s = new string('0', 50000);
cache.Add(new CacheItem(i.ToString(), s), p);
}
After about 30K iterations, I get a System.OutOfMemoryException.
Why doesn't MemoryCache and the GC release the objects stored when memory conditions are low?