Description
SqlCommand.Dispose
contains this code:
// release managed objects
_cachedMetaData = null;
This doesn't free the cached data, which is supposed to be the purpose of the Dispose(Boolean) overload when working with managed resources that don't implement IDisposable
:
Managed objects that consume large amounts of memory or consume scarce resources. Freeing these objects explicitly in the Dispose method releases them faster than if they were reclaimed non-deterministically by the garbage collector.
By just setting _cachedMetaData
to null, the resources are only reclaimed by the garbage collector, by which time the disposed SqlCommand
object would also likely not be referenced, meaning that disposing the SqlCommand
and setting _cachedMetaData
to null is wasted effort.
This issue was observed as part of a discussion about this question: Is SqlCommand.Dispose() required if associated SqlConnection will be disposed?