SQLConnection causes finalization for "Microsoft.Win32.SafeHandles.SafeTokenHandle" even on close connection RRS feed

  • Question

  • I am executing following code and checks the memory with CLR Profiler, it gives a large number of


    1#  Microsoft.Win32.SafeHandles.SafeTokenHandle"  objects as FInalized which were allocation from SqlConnection class.

          Allocation graph as



    2# finazlization for 1# happens only in case of WIndows integrated authentication. In case of using User name and Password in connection string no Finalization is shown


    3# System.Threading.ReaderWriterLock" object finalization allocation graph as



    4# A large number of "GC handles Surviving", which are allocated by SqlConnectin, SqlDataReader and SqlCommand



    for(int count = 0; count < 3000; count++)


    using (SqlCommand cmd = new SqlCommand(storedProcName))


              string  connectionString  = "Server=.; database=mydb;  Integrated Security=true";

    cmd.CommandType = CommandType.StoredProcedure;

    cmd.Connection = new SqlConnection(connectionString);


    using (IDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))


    ReadData(reader); //some business logic





    How to I avoid these finalizations and GC handles





    Saturday, August 11, 2007 10:28 AM