If you are seeing these impact GC, then that suggests you aren't using them properly. Of these, SqlDataReader
doesn't have a finalizer in the first place, so I assume we're talking about SqlCommand
and SqlConnection
. Both of these inherit from Component
, and in both cases the Dispose()
call unregisters them from GC - it is:
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
So: if you are setting these appear as finalizers in GC, look to see whether you are disposing your objects correctly. The easiest way to do that is via using
statements. For example:
using(var conn = OpenSomeConnection())
using(var cmd = conn.CreateCommand()) {
// TODO configure command
using(var reader = cmd.ExecuteReader()) {
// TODO consume data
}
}