Skip to content

Instantly share code, notes, and snippets.

@markryd
Created August 4, 2014 05:15
Show Gist options
  • Select an option

  • Save markryd/b301c36a21b9a20cbb1b to your computer and use it in GitHub Desktop.

Select an option

Save markryd/b301c36a21b9a20cbb1b to your computer and use it in GitHub Desktop.
Check if something will leak memory
//via http://stackoverflow.com/questions/578967/how-can-i-write-a-unit-test-to-determine-whether-an-object-can-be-garbage-collec
[Test]
public void MyTest()
{
WeakReference reference = null;
new Action(() =>
{
var service = new Service();
// Do things with service that might cause a memory leak...
reference = new WeakReference(service, true);
})();
// Service should have gone out of scope about now,
// so the garbage collector can clean it up
GC.Collect();
GC.WaitForPendingFinalizers();
Assert.IsNull(reference.Target);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment