How do I insure that SqlBulkCopy resources are cleaned up after use?
if i create new sqlbulkcopy object, vs 2008 , 2010 intellisense not show having .dispose() method and if type in the compiler won't allow it.
doesn't sqlbulkcopy create unmanaged resources should cleaned calling .dispose()? bol/vs indicate object implements .dispose(). code examples show object being declared in using block, don't believe using can right thing unless believes class has a .dispose() method.
i tried work around:
sqlbulkcopy bc = null; try { sqlbulkcopy bc = new sqlbulkcopy(); // stuff bc } finally { if (bc != null) ((idisposable)bc).dispose(); }
this @ least compiles (and runs). however, as watch application, copies a lot of sql tables, the process's memory usage keeps growing in task manager.
how insure sqlbulkcopy resources cleaned after use?
problem solved. found , fixed incorrect initialization of wpf text field, being used diagnostic messages related bulk copy , keeping infinite undo list (arrgh). having fixed that, still seeing reliable 25k leak per sqlbulkcopy run. looked @ generated il , found 3 patterns dealing object that implements .dispose(), generate correct call inside block in the il (the c# below):
callvirt instance void [mscorlib]system.idisposable::dispose()
the problem solved when stopped running under debugger. when run cmd.exe shell or double-clicking app, no more leak.
// try/finally pattern
sqlbulkcopy bc = null;
try
{
sqlbulkcopy bc = new sqlbulkcopy(dest_cn);
}
finally
{
if (bc != null)
((idisposable) bc.dispose();
}
// using pattern i
using (sqlbulkcopy bc = new sqlbulkcopy(dest_cn))
{
}
// using pattern ii (defensive) -- if handed bc , didn’t know if implemented .dispose()
sqlbulkcopy bc = new sqlbulkcopy(dest_cn);
using (bc as idisposable)
{
}
SQL Server > SQL Server Data Access
Comments
Post a Comment