静态特性和构造函数注入潜在的内存泄漏



我有一个在启动上运行的Bootstrapper任务。我得到一个注入构造函数的对象,一个单例,然后将静态属性设置为此对象。这会导致Boostrapper类不为GCD吗?

public class BootstrapperTask : IStartupTask
{
    public BootstrapperTask
    (
        ILocaliser<string> languageLocaliser
    )
    {
        //here I set the static property
        Local.LanguageLocaliser = languageLocaliser;
    }
    public async Task ExecuteAsync(CancellationToken cancellationToken = default)
    {
        // perform startup actions
    }
}

singleton ILocaliser<string>被注入构造函数languageLocaliser,该构建器引用了DI容器中的对象。Local静态类,然后引用languageLocaliser,它是BootstrapperTask的一部分。因为Local是静态的,并且会直到应用程序结束,这是否意味着BootstrapperTask不能因为Local指向languageLocaliser吗?

如果有对您的实例类的引用,

Local.LanguageLocaliser = languageLocaliser // reference 

然后,参考计数将大于0,因此不会收集垃圾

最新更新