Resources aren't garbage

December 12, 2008

Regarding Microsoft’s .NET OdbcConnection Class:

You should always explicitly close any open OdbcConnection objects by calling Close or Dispose before the OdbcConnection object goes out of scope, or by placing the connection within a Using statement. Not doing this leaves the freeing of these native resources to garbage collection. It might not free them immediately. This, in turn, can eventually cause the underlying driver to run out of resources or reach a maximum limit. This has resulted in intermittent failures. … Explicitly closing the connections allows for a more efficient use of native resources, enhancing scalability and improving overall application performance.

Good to know.

.NET has cool things about it, but this philosophy of “don’t worry about cleaning up after yourself–we’ve gotcha covered” still sticks in my craw.

You can’t save me from managing my own resources, no matter how hard you try. And I’m pretty convinced that having to be constantly mindful of a non-deterministic garbage-collector isn’t any better than just doing it all myself. (Call me old-school, I guess.)

See also Who Manages the Managed Language?