views:

816

answers:

3

Hello

In some my project I notice that during executing unit tests under VSTS2008 its VSTestHost's memory consuming grows. As I have very many tests in my solution it leads to OutOfMemroyException eventually. That looks very strange for me as I was sure that MSTest creates a new AppDomain for each unit test. Otherwise how would it reset static fields? But if AppDomain is being created for each test than memory shouldn't leak. But it does.

So the question is: Should VS create AppDomain for each test class or not? If yes than how can I check that it does it. I tried tracing through ProcessExpolorer and Performance snap-in. A value of "Total appdomain unloaded" is always 0 during test run.

+1  A: 

I don't think the unit test engine creates a new AppDomain for each test. Since creating an AppDomain is a relatively expensive operation, doing so for each test would slow down execution of unit tests considerably!

Visual Studio 2008 uses an seperate executable called vstesthost.exe to run unit tests. VS communicates with vstesthost.exe (how it does this I don't know) to tell it what tests to run. vstesthost.exe returns the execution results to VS which displays those results.

If you are getting OutOfMemoryExceptions when running your unit tests I would say that's a strong indicator that your code under test is actually not cleaning things up. Are you sure that you aren't retaining handles to unmanaged objects/memory? I would recommend running your unit tests under a Performance Analysis (you can do that by finding the unit test under the "Test View", right-clicking on it, and selecting "Create Performance Session"). This might shed some light at least on your object allocations.

Jeremy Wiebe
A: 

I was wrong about having separate AppDomains for each unittest.

Here's evidence: a singleton public class Singleton { public static Singleton Instance = new Singleton();

 private Guid _token;
 private Singleton()
 {
  _token = Guid.NewGuid();
 }

 public Guid Token
 {
  get { return _token; }
 }
}

and two tests:

[TestClass]
public class UnitTest2
{
 [TestMethod]
 public void TestMethod1()
 {
  Console.WriteLine(Singleton.Instance.Token);
 }
}
[TestClass]
public class UnitTest1
{
 [TestMethod]
 public void TestMethod1()
 {
  Console.WriteLine(Singleton.Instance.Token);
 }
}

During executing both tests output the same guid.

Shrike
+2  A: 

MsTest creates one-app domain per Test assembly, unless you are using noisolation, in which case there is no AppDomain Isolation.

If you are seeing leaks, its probably a but in either your test code, or your product code. Make sure you aren't stuffing things into dictionaries and leaving them there.

dhopton
+1, app domain per assembly. But - it also creates a new test class instance per test, so it should actually GC the fields. I have also some memory problems with MS test and I don't know why. I think there are some problems in the test runner.
Stefan Steinegger