CTZStef
CTZStef

Reputation: 1715

MSTest OutOfMemory Exception

Problem:

Recently I started to face an issue on one of my project that contains around 650 unit tests.

When I run all of them at once, some tests near the end of the unit test run start failing with a System.OutOfMemoryException.

When I re-run the failing test again, they pass.

Analysis:

I looked at the task manager while running my test to see that the vstest.executionengine.x86.exe always increases until it reach 1330 MB of memory. Then the tests starts failing with the out of memory exception.

Of course im sure that its possible that a few of my classes may have a memory leak, but 90% of my classes under test doesn't have such code that could lead to memory leaks.

Many of my tests class doesn't have any TestInitialize or TestCleanup method since the creation of my classes are done directly inside the TestMethod test and since those classes under test doesn't implement IDiposable (since there is no code that could lead to leaks).

Questions:

Now I am not sure what I might be doing wrong. But I'm wondering how come test objects remains in memory even after all the tests of a test class were run?

Has anyone experienced this or could point me to what I should/could do to fix or locate the problem?

Thanks!

Upvotes: 1

Views: 1943

Answers (2)

CitrusO2
CitrusO2

Reputation: 906

We had a similar issue: Turns out, when running the tests on our CI Server using TFS and the Visual Studio Test Agent Deployment and Run Functional Tests tasks AND the build is done with Release (not Debug) the testrunner does not seem to collect the memory after each test. We added a manual cleanup and a manual GC collect at the testcleanup, instead of getting a out of memory exception ~1,5GB memory usage we now stay around 300MB memory usage.

Upvotes: 2

Ed Pavlov
Ed Pavlov

Reputation: 2508

Reference dotMemory Unit framework from your test project and add following code to the test running last before OOM occured (or somewhere close to OOM).

dotMemoryApi.GetSnapshot();
dotMemoryApi.SaveCollectedData();

Then open this snapshot with dotMemory profiler and look what wastes a memory. Also look at Large Object Heap, it's possible you have a problem with its fragmentation.

Upvotes: 3

Related Questions