内存泄漏:幕后到底发生了什么?

时间:2019-01-22 08:10:50

标签: c# memory-leaks autofac serilog

我有一个应用程序经常由于内存泄漏而停止。

ContainerFactory

public class ContainerFactory: IContainerFactory
{
    public TTypeOfInstance GetInstance<TTypeOfInstance>()
    {
        return _container.Resolve<TTypeOfInstance>();
    }
    public TTypeOfInstance GetLoggerInstance<TTypeOfInstance>()
    {
        using (var lifetimeScope = _container.BeginLifetimeScope())
        {
            return lifetimeScope.Resolve<TTypeOfInstance>();
        }
    }

    public void ScopedInstance<TTypeOfInstance>(Action<TTypeOfInstance> method)
    {
        using (var scopedInstance = _container.BeginLifetimeScope())
        {
            var instance = scopedInstance.Resolve<TTypeOfInstance>();
            method(instance);
        }
    }
  }

我正在按照以下说明在构造函数中创建logger的实例,并导致memory leak和我的应用每隔一天失败

    public MyClass(IContainerFactory containerFactory)
    {
        _containerFactory = containerFactory;
        _logger = _containerFactory.GetInstance<ILogger>().ForContext(GetType());
    }

如果我按照以下方式更改代码,不会有任何内存泄漏

  logger = containerFactory.GetLoggerInstance<ILogger>().ForContext(GetType()); 

那么BeginLifetimeScope到底在做什么,以及它如何帮助解决内存问题?

而且,创建实例是否正确?有什么更好的方法吗?

这是内存泄漏

enter image description here

这是例外

  

框架版本:v4.0.30319   说明:由于未处理的异常,进程已终止。   异常信息:System.AccessViolationException   在Serilog.Events.DictionaryValue..ctor(System.Collections.Generic.IEnumerable1>)   在Serilog.Capturing.PropertyValueConverter.TryConvertEnumerable(System.Object,Serilog.Parsing.Destructuring,System.Type,Serilog.Events.LogEventPropertyValue ByRef)at Serilog.Capturing.PropertyValueConverter.CreatePropertyValue(System.Object, Serilog.Parsing.Destructuring, Int32) at Serilog.Capturing.MessageTemplateProcessor.CreateProperty(System.String, System.Object, Boolean) at serilog.Exceptions.Destructurers.ExceptionEnricher.Enrich(Serilog.Events.LogEvent, Serilog.Core.ILogEventPropertyFactory)

这是记录器的配置方式

    public static IContainerFactory BuildContainer()
    {
        var containerFactory = new ContainerFactory();
        var appSettings = ConfigurationManager.AppSettings;
        var applicationName = appSettings.Get("applicationName");
         builder.RegisterInstance(containerFactory).As<IContainerFactory>();
        builder.Register(c => GetLoggerConfiguration(applicationName).CreateLogger()).As<ILogger>();
    }


    private static LoggerConfiguration GetLoggerConfiguration(string applicationName)
    {
        var config = new LoggerConfiguration();
        if (!string.IsNullOrWhiteSpace(Settings.Default.ApplicationInsightsInstrumentationKey))
        {
            config = config.WriteTo.ApplicationInsightsEvents(Settings.Default.ApplicationInsightsInstrumentationKey, LogEventLevel.Error);
        }

        config = config.
            Enrich.WithProperty("ApplicationName", applicationName).
            Enrich.WithExceptionDetails().
            Enrich.WithMachineName().
            Enrich.WithProcessId().
            Enrich.WithThreadId().
            ReadFrom.AppSettings();

        return config;
    }

0 个答案:

没有答案