MemoryCache.Dispose Method

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

Namespace:  System.Runtime.Caching
Assembly:  System.Runtime.Caching (in System.Runtime.Caching.dll)

public void 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.

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft