本文介绍了为什么EF Core 2.2.6不会进行垃圾收集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用dotMemoryUnit来证明我的DbContext对象正在正确地收集垃圾。

I am using the dotMemoryUnit to prove the my DbContext object is getting garbage collected properly.

我觉得这段代码在单元测试中应该可以正常工作,但是测试总是失败。我唯一能猜到的是EF Core在某个地方保存了一个引用。

I feel that this code should work properly in a unit test, but the test always fails. The only thing I can guess is EF Core is holding a reference somewhere.

编辑:我不确定建议的解决了此问题,因为它是.Net Core而不是.Net Framework。 GC.Collect()默认值的文档是强制性的,文档对提示没有任何说明。

I'm not sure the suggested question addresses this problem as this is .Net Core and not .Net Framework. The documentation for GC.Collect()'s default is forced and the documentation says nothing about hints.

编辑2:我确实在下面找到了答案。

Edit 2: I did find the answer below.

有何想法?

    public class UnitTest1
    {
        public UnitTest1(ITestOutputHelper output)
        {
            DotMemoryUnitTestOutput.SetOutputMethod(output.WriteLine);
        }

        [Fact]
        [DotMemoryUnit(FailIfRunWithoutSupport = false)]
        public void DummyContext_DisposesContextOnGarbageCollect()
        {
            // Arrange
            var options = new DbContextOptionsBuilder<DummyContext>()
                .UseSqlServer("data source=ASqlServer;Integrated Security=true");

            using (var ctx = new DummyContext(options.Options))
            {
                // do nothing
            }

            GC.Collect();
            GC.WaitForPendingFinalizers();

            dotMemory.Check(
                memory =>
                    Assert.Equal(
                        0,
                        memory.GetObjects(where => where.Type.Is<DummyContext>()).ObjectsCount));
        }

        private class DummyContext : DbContext
        {
            public DummyContext(DbContextOptions options)
                : base(options)
            {
            }
        }
    }


推荐答案

再进行一些研究后,我在

After a little more research, I have found the answer in this post from Jet Brains:

帖子建议将代码包装在一种操作方法中。

The post suggests wrapping the code in an action method.

对于其他可能遇到此问题的人,以下是我的新测试方法:

For others that may have this problem, below is my new test method:

[Fact]
[DotMemoryUnit(FailIfRunWithoutSupport = false)]
public void DummyContext_DisposesContextOnGarbageCollect()
{
    var isolator = new Action(
        () =>
        {
            // Arrange
            var options = new DbContextOptionsBuilder<DummyContext>()
                .UseSqlServer("data source=ASqlServer;Integrated Security=true");

            using (var ctx = new DummyContext(options.Options))
            {
                // do nothing
            }
        });

    isolator();

    GC.Collect();
    GC.WaitForPendingFinalizers();

    SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);

    dotMemory.Check(
        memory =>
            Assert.Equal(
                0,
                memory.GetObjects(where => where.Type.Is<DummyContext>()).ObjectsCount));
}

这篇关于为什么EF Core 2.2.6不会进行垃圾收集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!