MemoryCache get in Disposed state Magically RRS feed

Todas as Respostas

  • This might be happening if ASP.NET AppDomain is unloaded then handler of the AppDomainUnloaded in memory cache instance will call dispose.

    Since ASP.NET Cache is meant for web applications, I don't know is this bug in MemoryCache or some design that you should consider if using it in web application. Documentation does not give any instructions how to use MemoryCache in web applications so I have assumed you should not use it.

    segunda-feira, 3 de setembro de 2012 12:44
  • MasaSam, I thought could be the AppDomain Unload, but after some investigation I don´t think that is it:

    1-) MSDN says to use MemoryCache for ASP.NET:

    And says that ASP.NET 4 cache is built with ObjectCache:

    In ASP.NET 4, caching is implemented by using the ObjectCache class.

    2-) AppDomain unload would reset statics variable, creating new MemoryCache instances.

    3-) I attached a event to AppDomain Unload like the MemoryCache does(I saw the decompiled source) in my test it wasn´t called and memorycache get  disposed. - MCPD em .NET 2.0 MCTS em .NET 4

    terça-feira, 4 de setembro de 2012 01:10
  • A workaround (not a good one) is to change Pooling Interval to something like 30 hours(since default IIS recycle time is 27h).

    I realized that MemoryCache is getting disposed after 2 minutes, the default Pooling Interval.

    It´s working for 10 hours until now. Before it have worked for 20minutes at best case. - MCPD em .NET 2.0 MCTS em .NET 4

    terça-feira, 4 de setembro de 2012 01:21
  • I have recently had this issue on a Webapp also. However I have been using MemoryCache just fine on old IIS6 configuration. When moving to IIS7 I noticed this problem. Therefore I played around a little bit and found this is further related to running in Integrated (managed pipeline mode) as opposed to Classic and another 'fix' is to change back to Classic mode.

    Not only does this issue cause your Cache to become disposed after the polling interval, I also noticed that this can adversely affect some Performance counters - such as .Net Data Provider for SqlServer which also seems to disappear for that webapp pool instance after the polling interval and the MemoryCache gets disposed.

    I looked for error or warnings in event viewer and turned on error tracing but no error or warning is thrown the object is just disposed after the polling interval.

    Perhaps there is something in my webapp life-cycle that is incompatible with Integrated managed pipeline and the MemoryCache implementation but I really have no idea what it could be.

    Other solution presented are to use httpruntime cache or httpcontext caches but I am really curious as to why this doesn't work going from Classic -> Integrated and in my case IIS6 to IIS7.

    Is there any reason not to run in Classic mode given this works?

    Thanks for any ideas and I hope the extra info is of use to someone.

    quarta-feira, 26 de setembro de 2012 12:50
  • I filed a bug on Connect with a reproducer and some more info on reproducing.

    Basically needs to be windows Auth webapp, app pool running as a specific user, Integrated pipeline mode and then your memorycache gets disposed after polling interval

    I wonder if the app pool user or the auth'd user does not have permissions to do memory management on cache or if there is some timeout on the app pool user in integrated mode?
    terça-feira, 23 de outubro de 2012 10:39
  • So, here's some news. We looked into this and YES, this is a bug in .NET 4.

    The good news is that it was fixed in .NET 4.5, so if you can, update your installation to .NET 4.5 and you're solid.

    The other good news it that this fix has been back-ported to .NET 4 and will be available as a QFE (Quick Fix...a one off fix you'll apply) #578315. It was backported/fixed and it should be out ASAP. I'll try to get an exact date, but it's soon.

    The other other good news is that there's a workaround for this on .NET 4 before the QFE. The workaround is weird, but it could unblock you.

    using (ExecutionContext.SuppressFlow()) {
       // Create memory cache instance under disabled execution context flow             
       return new YourCacheThing.GeneralMemoryCache(…);

    Hope this helps.


    Scott Hanselman

    sábado, 30 de março de 2013 07:08
  • I have the same problem. After 2 min (Pooling interval) my cache is cleaned up. I added RemovedCallback for each cache item and found out that my cache items were evicted.

    CacheItemPolicy p = new CacheItemPolicy();
    p.RemovedCallback = new CacheEntryRemovedCallback(MyCachedItemRemovedCallback);
    memoryCache.Set(key, value, p);
    private static void MyCachedItemRemovedCallback(CacheEntryRemovedArguments arguments)
       Logger.log.InfoFormat("Cach {0} was removed because: {1}", arguments.CacheItem.Key, arguments.RemovedReason.ToString());

    I red in msdn ( that eviction is related to cacheMemoryLimitMegabytes and physicalMemoryLimitPercentage: "A cache entry was removed to free memory in the cache. This occurs when a cache instance approaches cache-specific memory limits, or when a process or cache instance approaches computer-wide memory limits." So I tried to set cacheMemoryLimitMegabytes=7Gb (total RAM on PC is 8Gb), but looks like it doesn't work. 

    So I set PoolingInterval to 1 hour (I have cache reload interval equals to 15 min) and it helped. In other words I say MemoryCache: do not think about RAM. (it is bad :((( but it works)

    terça-feira, 13 de junho de 2017 14:49
  • PS: I use framework 4.5
    quarta-feira, 14 de junho de 2017 09:04