Created
August 4, 2014 05:15
-
-
Save markryd/b301c36a21b9a20cbb1b to your computer and use it in GitHub Desktop.
Check if something will leak memory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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