IDisposable Best Practices
for C# Developers
Introducing IDisposable
Elton Stoneman
geekswithblogs.net/eltonstoneman
@EltonStoneman
Introducing IDisposable
Memory Databases Network
Introducing IDisposable
Process allocates memory
AppDomain Heap
Stack
Introducing IDisposable
Process allocates memory
AppDomain Heap
Stack
{...} int32
Introducing IDisposable
Process allocates memory
AppDomain
Heap Runs processing
obj1
Stack
{...} int32
{...} pointer
Introducing IDisposable
Process allocates memory
AppDomain
Heap Runs processing
obj1
Stack
{...} int32
{...} pointer obj2
{...} pointer
Introducing IDisposable
Process allocates memory
AppDomain
Heap Runs processing
obj1
Stack
{...} int32
{...} pointer obj2
{...} pointer
Introducing IDisposable
Process allocates memory
AppDomain Heap Runs processing
Stack
Releases memory
Introducing IDisposable
Process allocates memory
AppDomain
Heap Runs processing
obj1
Stack
{...} int32
{...} pointer obj2
{...} pointer
Introducing IDisposable
Process allocates memory
AppDomain
Heap Runs processing
Garbage obj1
Collector Garbage collector
obj2
Introducing IDisposable
Process allocates memory
AppDomain
Heap Runs processing
Garbage obj1
Collector Garbage collector
obj2
Introducing IDisposable
Process allocates memory
AppDomain
Heap Runs processing
Garbage obj1
Collector Garbage collector
Releases memory
Introducing IDisposable
Process allocates memory
AppDomain
Heap Runs processing
Garbage obj1
Collector Garbage collector
Releases memory
Introducing IDisposable
Process allocates memory
AppDomain Heap Runs processing
Garbage
Collector Garbage collector
Releases memory
Not Using IDisposable?
6
Expanding memory
w
Hogging external
b
Functional defects
profile resources
Introducing IDisposable
IDisposable Using and Demo with file IO,
interface implementing SQL and WCF
IDisposable
Course Outline
What Happens What Happens
Introducing
when the GC if you don't
IDisposable
Runs? Dispose?
Summary
You are here
Simple Interfaces
public interface IDontDoAnything
{
}
public class DoesntDoAnything : IDontDoAnything
{
}
Simple Interfaces
public interface IDoOneThing
{
void DoTheThing();
}
public class DoesOneThng : IDoOneThing
{
public void DoTheThing()
{
}
}
Simple Interfaces
namespace System
{
public interface IDisposable
{
void Dispose();
}
}
IDisposable
provides a mechanism for
releasing unmanaged
resources
Unmanaged Resources
Runtime-callable wrapper
Managed code External COM library
Interop Explicitly unmanaged
DllImport
PInvoke
Native library IntPtr
Unmanaged Resources
Managed access
Managed code External resources
IO Data Svc Implicitly unmanaged
System.IO
System.Data
System.ServiceModel
Using IDisposable
public class MayUseUnmanagedResources : IDisposable
{
public void Method() { /* ... */ }
public void Dispose() { /* ... */ }
}
static void Main()
{
using (var obj = new MayUseUnmanagedResources())
{
obj.Method();
}
}
Demo 1: SqlConnection
Demo 1: SqlConnection
public class DatabaseState : IDisposable
{
private SqlConnection _connection;
/* ... */
}
using (var state = new DatabaseState())
{
state.GetDate().Dump();
}
Demo 1: SqlConnection
public class DatabaseState : IDisposable
{
private SqlConnection _connection;
/* ... */
public void Dispose()
{
_connection.Close();
_connection.Dispose();
}
}
Demo 1: SqlConnection
using (var command = _connection.CreateCommand())
{
command.CommandText = "SELECT getdate()";
return command.ExecuteScalar().ToString();
}
Best Practice #1
Dispose of IDisposable
objects as soon as you can
✮
Best Practice #1
using (var state = new DatabaseState())
{
state.GetDate().Dump();
}
var state = new DatabaseState();
try
{
state.GetDate().Dump();
}
finally
{
state.Dispose();
}
The Word Counting App
Σ(B) = 40,000 words
The Word Counting App
Everything Else
Managed Code
Demo 2: The Word Counting App
Feature Walkthrough Walkthrough
Compute number Verify file drops App failing
of words in text have word count because of
file & store count persisted neglecting
in database IDisposable
Demo 2: The Word Counting App
b
Summary
Introducing IDisposable
Interface definition
Meaning
Unmanaged resources
Native code– IntPtr & interop
*And* managed code
Which implements IDisposable What Happens
May or may not use unmanaged resources when the GC
Runs?
Demo solution
File IO, SQL and WCF