MemoryCache.Dispose Method


Releases all resources that are used by the current instance of the MemoryCache class.

 virtual void Dispose();
public void Dispose ();
abstract member Dispose : unit -> unit
override this.Dispose : unit -> unit
Public Sub Dispose ()



Each MemoryCache instance is bound to the AppDomainUnloaded event. However, during application-domain shutdown, if a memory-based cache has not been explicitly disposed, the cache instance will automatically call the Dispose method.

The disposed cache instance is shut down using the following steps:

  1. The state of the cache is set to indicate that the cache is disposed. Any attempt to call public caching methods that change the state of the cache, such as methods that add, remove, or retrieve cache entries, might cause unexpected behavior. For example, if you call the Set method after the cache is disposed, a no-op error occurs. If you attempt to retrieve items from the cache, the Get method will always return null.

  2. Performance counter information is no longer raised from the current cache instance.

  3. All references to cached objects held by the current cache instance are released.

  4. Any CacheEntryChangeMonitor instances that are currently monitoring entries in the cache are notified.

  5. Callback objects that provide notification of changes for those monitors are called.

  6. Any CacheItemRemovedCallback instances that are registered with entries in the cache are called. The removal reason that is passed to the callbacks is CacheSpecificEviction.

Applies to