Skip to content

SqlCommand.Dispose doesn't free managed object #74

Open
@breyed

Description

@breyed

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions