I am writing an acceptance test for my managed application to ensure it is not leaking memory when loading and unloading data.
Here is a sample code of what I am doing:
System.Diagnostics.Process process = System.Diagnostics.Process.GetCurrentProcess();
long memoryBefore = process.PrivateMemorySize64;
// Keep creating new projects.
for (int i = 0; i < 100; i++){
MyApplication.CreateObject();
MyApplication.UnlaodLastCreatedObject();
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
System.GC.WaitForFullGCComplete();
}
To test if my process has the same memory consumption (+1MB), I do the following:
// Refresh the memory statistics
process.Refresh();
Assert.Less((decimal)process.PrivateMemorySize64,(decimal)(memoryBefore + (1024 * 1024)));
This tests seems to be not reliable at all as it randomly fails from time to time.
Anything wrong with my code above? Is there a different way to test if my process is not leaking memory?
EDIT
The failures are random, below is a screenshot of my build server output where you can clearly see the test failing from time to time.

Thank you.
There are two things that might be helpful for You. First, have a look on msdn for
GC.WaitForFullGCCompletemethodhttp://msdn.microsoft.com/pl-pl/library/cc647068.aspx
It says, that
Second thing is, if you have any objects, that implements Finalize methods, they won’t be collected in first garbage collection (you can read a bit about freachable queue to get more overview how objects with Finalize methods work).