MS测试与测试 应用程序域

发布于 2024-07-08 10:15:04 字数 373 浏览 4 评论 0原文

在我的一些项目中,我注意到在 VSTS2008 下执行单元测试期间,其 VSTestHost 的内存消耗会增加。 由于我的解决方案中有很多测试,因此最终会导致 OutOfMemroyException 。 这对我来说看起来很奇怪,因为我确信 MSTest 为每个单元测试创​​建一个新的 AppDomain。 否则它如何重置静态字段? 但是,如果为每个测试创建 AppDomain,则内存不应泄漏。 但确实如此。

所以问题是:VS 是否应该为每个测试类创建 AppDomain? 如果是的话我该如何检查它是否做到了。 我尝试通过 ProcessExplorer 和性能管理单元进行跟踪。 在测试运行期间,“Total appdomain unloaded”的值始终为 0。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

笑饮青盏花 2024-07-15 10:15:04

MsTest 为每个测试程序集创建一个应用程序域,除非您使用噪声隔离,在这种情况下,没有应用程序域隔离。

如果您发现泄漏,则可能是您的测试代码或产品代码中的“但是”。 确保你没有把东西塞进字典然后把它们留在那里。

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.

幼儿园老大 2024-07-15 10:15:04

我不认为单元测试引擎会为每个测试创建一个新的 AppDomain。 由于创建 AppDomain 是一项相对昂贵的操作,因此为每个测试都这样做会大大减慢单元测试的执行速度!

Visual Studio 2008 使用名为 vstesthost.exe 的单独可执行文件来运行单元测试。 VS 与 vstesthost.exe 通信(我不知道它是如何做到这一点的),告诉它要运行哪些测试。 vstesthost.exe 将执行结果返回给 VS,VS 显示这些结果。

如果您在运行单元测试时遇到 OutOfMemoryExceptions,我会说这是一个强有力的指标,表明您的测试代码实际上没有清理问题。 您确定没有保留非托管对象/内存的句柄吗? 我建议在性能分析下运行单元测试(您可以通过在“测试视图”下找到单元测试,右键单击它,然后选择“创建性能会话”来实现)。 这至少可能对您的对象分配有所帮助。

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 a 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.

︶葆Ⅱㄣ 2024-07-15 10:15:04

我错误地为每个单元测试设置了单独的 AppDomain。

证据如下:
一个单例

public class Singleton
{
    public static Singleton Instance = new Singleton();

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

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

和两个测试:

[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);
    }
}

在执行期间,两个测试输出相同的 guid。

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.

风筝在阴天搁浅。 2024-07-15 10:15:04

在大型测试运行中也看到了同样的问题。 我的理论如下。 本例中的内存耗尽是因为 MSTest 测试结果文件是 XML。 因此,它需要将所有日志结果保留在内存中,直到测试运行结束,然后再序列化到磁盘。 XML 万岁:-)

我已将此问题发布为 连接问题 不久前,它应该已经在 MSTest 10(64 位)中得到修复,但我还无法验证这一点,因为我们已经转移到 VS2010 和 .NET 4.0 的所有其他问题。

Seen the same problem with large test runs. My theory is the following. Memory exhaustion in this case is due to the fact that MSTest test result files are XML. Therefore it needs to keep all the log results in memory until the end of the test run before serializing to disk. Hurray for XML :-)

I have posted this problem as a connect issue a while back and it should have been fixed in MSTest 10 (going 64 bit) but I haven't been able to verify this yet because of all the other problems we have moving to VS2010 and .NET 4.0.

陌路终见情 2024-07-15 10:15:04

处理单例的唯一方法是处理 appDomain。 单例是一个静态的自身,所以它基本上是一个循环引用。 真正的单例在应用程序域消失之前不会被释放。

The only way to dispose of a singleton is to dispose the appDomain. A singleton is a static holding onto itself, so it's basically a circular reference. True singletons do not get disposed until the appdomain goes away.

葮薆情 2024-07-15 10:15:04

这似乎在 MSTest 2010 中没有得到解决。我遇到了很多类似的问题。 为什么垃圾收集在单元测试中不起作用?

我的理解是,UT 框架负责处理所有已执行的测试,但我们代码中的一些单例模式似乎并非如此。

This does not seem to be solved in MSTest 2010. I am experiencing a lot of similar issues like this. Why does garbage collection not work in unit test?

My understanding was that the UT framework took care of disposing of all executed tests, but this does not seem to be the case with some singleton patterns that we have in code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文