I have an application in which a lot of memory leaks are present. For example if a open a view and close it 10 times my memory consumption rises becauses the views are not compl
You don't need unit tests you need memory profiler. You can start with CLR Profiler.
That memory consumption increases is not necessarily an indication of a resource leak, since garbage collection is non deterministic and may not have kicked in yet. Even though you "let go" of objects, the CLR is free to keep them around as long as it deems enough resources are available on the system.
If you know you do in fact have a resource leak, you may work with objects that have explicit Close/Dispose as part of their contract (meant for "using ..." constructs). In that case, if you have control over the types, you can flag disposal on the objects from their Dispose implementation, to verify that they have in fact been disposed, if you can live with lifecycle management leaking into the type's interface.
If you do the latter, it is possible to unit test that contractual disposal takes place. I've done that on some occasions, using an application specific equivalent to IDisposable (extending that interface), adding the option for querying whether the object has been disposed. If you implement that interface explicitly on your type, it won't pollute its interface as much.
If you have no control over the types in question, a memory profiler, as suggested elsewhere, is the tool you need. (For instance dotTrace from Jetbrains.)
You might be able to hook into the profiling API but it looks like you would have to start your unit tests up with profiler enabled.
How are the objects being created? Directly or some way that can be controlled. If controllable return extended versions with finalizers which register that they have been disposed. Then
GC.Collect();
GC.WaitForPendingFinalizers();
Assert.IsTrue(HasAllOfTypeXBeenFinalized());
How about something like:
long originalByteCount = GC.GetTotalMemory(true);
SomeOperationThatMayLeakMemory();
long finalByteCount = GC.GetTotalMemory(true);
Assert.AreEqual(originalByteCount, finalByteCount);
Often memory leaks are introduced when managed types use unmanaged resources without due care.
A classic example of this is the System.Threading.Timer
which takes a callback method as a parameter. Because the timer ultimately uses an unmanaged resource a new GC root is introduced which can only be released by calling the timer's Dispose
method. In this case your type should also implement IDisposable
otherwise this object can never be garbage collected (a leak).
You can write a unit test for this scenario by doing something similar to this:
var instance = new MyType();
// ...
// Use your instance in all the ways that
// may trigger creation of new GC roots
// ...
var weakRef = new WeakReference(instance);
instance.Dispose();
instance = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
Assert.IsFalse(weakRef.IsAlive);
dotMemory Unit framework has capabilities to programmatically check amount of certain objects allocated, memory traffic, make and compare memory snapshots.