Reputation: 1425
I'm looking for a way to hook in to the garbage collection routine in ASP.NET's caching mechanism. As a test I put the following code in a .aspx file. I would expect the CacheItemRemoveCallback to be fired but it isn't. Does anyone know why? Is it because I'm running under MS Web Development Server instead of a full fledged instance of IIS?
private static List<string> _bigData = new List<string>();
protected void Page_Load(object sender, EventArgs e)
{
Cache.Add("a", " ".PadRight(1048576), null, DateTime.Now.AddYears(1), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Low,
new System.Web.Caching.CacheItemRemovedCallback((s, o, r) => {
// This code is never run
}));
while (true)
{
_bigData.Add(" ".PadRight(1048576));
Thread.Sleep(50);
}
}
Upvotes: 0
Views: 74
Reputation: 124726
I would expect the CacheItemRemoveCallback to be fired
Why do you expect that?
I'd expect you to get an OutOfMemoryException
fairly quickly on a 32-bit machine (*) - after a minute or so, by which time you'll have approx 1.2GB in your list.
(*) unless the OS is started with the /3GB switch, in which case it will behave similarly to a 32-bit process on a 64-bit machine.
On a 64-bit machine, your request will time when the a default of 90 seconds, by which time it will have added 90*200 = 1800 items = approx 1.8GB to your static list. A 64-bit process will handle this, and probably a 32-bit process would be able to do so on a 64-bit machine if it is LARGEADDRESSAWARE, which is definitely the case for IIS; not sure about Cassini.
Also, ISS would probably recycle your application domain when it hits this level of virtual memory usage, depending on how the application pool is configured.
I'd change your test to repeatedly add items to the cache, rather than a static list.
Upvotes: 1