2016-09-08 1 views
1

autofac은 작업 단위 중에 생성 된 모든 구성 요소를 처리하는 방법으로 수명 범위를 사용합니다. 이것이 강력한 기능인 반면 평생 범위를 적절히 처분하지 않는 코드를 작성하기 쉽기 때문에 시간이 지남에 따라 추적되는 disposables가 늘어나게됩니다. 사실상 메모리 누수가 발생합니다.Autofac 수명으로 추적되는 disposables 열거

어떤 시점에서 평생 범위에 의해 추적되는 일회용 개체의 수를 모니터링하는 방법이 있습니까? 나는 작업 단위에 일회용품을 적절히 할당하지 않는 것과 관련된 문제를 찾도록 도와주는 글쓰기 도구에 관심이 있습니다. 지금은 누수를 찾기 위해 메모리 프로파일 러 도구를 사용하지만 이것은 매우 번거로운 작업입니다.

ILifetimeScope의 공용 인터페이스를 살펴 보았지만 사용중인 것은 아무것도 표시되지 않습니다.

답변

1

불행히도 분석은 현재 Autofac의 약점 중 하나입니다. There is a repository where some work was started on an analytics package하지만 확실히 약간의 차이가 있습니다 - 예를 들어, 객체가 을 활성화 수명 범위 배치 할 때 당신이 볼 수있는 경우 추적 할 수 있지만, 개별 객체이의 일부로 배치 할 때 추적 할 수 없습니다 범위. 그것에 대한 이벤트가 없습니다. 정품 인증에 대한

아주 간단한 추적 모듈은 다음과 같을 것이다 :

using System; 
using System.Collections.Generic; 
using System.Linq; 
using Autofac; 
using Autofac.Core; 

namespace DiagnosticDemo 
{ 
    public class TrackingModule : Module 
    { 
    private readonly IDictionary<Type, int> _activations = new Dictionary<Type, int>(); 

    private readonly object _syncRoot = new object(); 

    public void WriteActivations() 
    { 
     foreach (var pair in this._activations.Where(p => p.Value > 0)) 
     { 
     Console.WriteLine("* {0} = {1}", pair.Key, pair.Value); 
     } 
    } 

    protected override void AttachToComponentRegistration(
     IComponentRegistry componentRegistry, 
     IComponentRegistration registration) 
    { 
     if (registration.Ownership == InstanceOwnership.OwnedByLifetimeScope) 
     { 
     registration.Activated += this.OnRegistrationActivated; 
     } 
    } 

    private void OnRegistrationActivated(
     object sender, 
     ActivatedEventArgs<object> e) 
    { 
     if (e.Instance is IDisposable) 
     { 
     var type = e.Instance.GetType(); 
     Console.WriteLine("Activating {0}", type); 
     lock (this._syncRoot) 
     { 
      if (this._activations.ContainsKey(type)) 
      { 
      this._activations[type]++; 
      } 
      else 
      { 
      this._activations[type] = 1; 
      } 
     } 
     } 
    } 
    } 
} 

당신이 뭔가를 같이 사용할 것이다 : 그러나

static void Main(string[] args) 
{ 
    var trackingModule = new TrackingModule(); 

    var builder = new ContainerBuilder(); 
    // Register types, then register the module 
    builder.RegisterType<Consumer>().As<IConsumer>(); 
    builder.RegisterType<DisposableDependency>().As<IDependency>(); 
    builder.RegisterType<NonDisposableDependency>().As<IDependency>(); 
    builder.RegisterModule(trackingModule); 
    var container = builder.Build(); 

    // Do whatever it is you want to do... 
    using (var scope = container.BeginLifetimeScope()) 
    { 
    scope.Resolve<IConsumer>(); 
    } 

    // Dump data 
    Console.WriteLine("Activation totals:"); 
    trackingModule.WriteActivations(); 
} 

, 이것은 어떤 항목을 알려하지 않을 이 아니므로 알고 싶습니다. 그래도 아이디어를 얻거나 적어도 도움이 될 수 있습니다.

Autofac에서 분석 기능을 개선하는 데 관심이 있으시면 take PRs or specific design ideas for how to improve입니다.

1

Whitebox은 찾고있는 것을 거의 수행하는 Autofac 용 GUI 진단 도구입니다. 최신 버전의 Autofac에 대한 업데이트가 필요할 수도 있습니다. 프로젝트를 다시 시작하고 실행하는 데 도움이 될 것입니다. 일부 docs on the old Google Code page.

+0

링크를 가져 와서 링크 된 autofac-analytics 저장소로 이식하기 시작하지 않았습니까? 델타 란 무엇입니까? –

+0

애널리틱스 저장소가 현재 양식에서 실제로 사용할 수 없다고 생각합니다. 화이트 박스는 아직 완전히 완성 된 방법이라고 생각합니다. (애널리틱스가 앞으로 나아지지 않으면 나는 그것을 놓쳤습니다 :-)) –