Microsoft Unity - DerivedTypeConstructorSelectorPolicy内存泄漏

时间:2012-12-05 08:49:07

标签: .net memory-leaks unity-container

因此,我们遇到了容器在应用程序期间持有DerivedTypeConstructorSelectorPolicy实例的问题。如果不是因为TypeInterceptionStrategy中的以下两行代码将旧策略包装成一个新策略,那么这就不会那么糟糕了,然后它现在保存到两个实例(或更多,每次调用Resolve以获取截获的类)会使问题复杂化)。运行.NET Memory Profiler时可以看到这一点。

IConstructorSelectorPolicy originalConstructorSelectorPolicy = PolicyListExtensions.Get<IConstructorSelectorPolicy>(context.Policies, (object) context.BuildKey, out containingPolicyList);
PolicyListExtensions.Set<IConstructorSelectorPolicy>(containingPolicyList, (IConstructorSelectorPolicy) new TypeInterceptionStrategy.DerivedTypeConstructorSelectorPolicy(proxyType, originalConstructorSelectorPolicy), (object) context.BuildKey);

当你有很多类被截获时,这会导致Gen 2集合的颠簸,然后你会在内存中出现GC无法回收的漏洞,特别是在复制策略以确保线程安全时。一旦你在政策清单中有足够数量的物品,它就会转移到LOH,这会使情况更糟。

我应该注意这一点,因为我们的应用程序在IIS Classic中运行,而不是在集成管道中运行,在32位模式下,我知道双重打击。在64位模式下具有更多可寻址空间,这不会那么糟糕。但是,我们仍然坚持使用IIS提供给我们的虚拟内存,因为GC无法在VM映射中找到足够的空间来分配额外的内存并且请求开始死亡,因此这种情况很快就会发生。

该文件只有3个版本,原始版本看起来没有这个问题。有没有其他人遇到这个问题,或者我错过了一些可能解释这种行为的东西?

指向这个的非常简单的程序的链接发布在pastebin:http://pastebin.com/DYG3GXNm

使用.NET Memory Profiler(或您选择的探查器)并观察DerivedTypeConstructorSelectorPolicy,您可以观察它在每次迭代中的增长情况,并且随着它的增长,您可以检查并查看originalConstructorSelectorPolicy继续引用长链中的旧实例

作为我们拦截了多少课程的一个例子,大约有1300个左右的注册顺序。

1 个答案:

答案 0 :(得分:1)

我在此期间找到了解决方案。这是一个简单的修复,但它要求您重写Interception和TypeInterceptionStrategy。该修复程序只是一个单行程序,只涉及检查策略列表中出现的类型。

这是TypeInterceptionStrategy的代码:

if (originalConstructorSelectorPolicy is DefaultUnityConstructorSelectorPolicy)
{
    containingPolicyList.Set<IConstructorSelectorPolicy>(new CustomDerivedTypeConstructorSelectorPolicy(proxyType, originalConstructorSelectorPolicy), context.BuildKey);
}

当然,为了改变它,你必须复制TypeInterceptionStrategy并执行与修复相同的操作,它不能简单地通过覆盖PreBuildUp方法来修复。

我正在粘贴整个修复程序,以防其他人遇到问题。

public class CustomTypeInterceptionStrategy : BuilderStrategy
{
    public override void PreBuildUp(IBuilderContext context)
    {
        Guard.ArgumentNotNull(context, "context");

        if (context.Existing != null)
        {
            return;
        }

        Type type = context.BuildKey.Type;
        ITypeInterceptionPolicy typePolicy = FindInterceptionPolicy<ITypeInterceptionPolicy>(context);

        if (typePolicy == null)
        {
            return;
        }

        ITypeInterceptor interceptor = typePolicy.GetInterceptor(context);

        if (!interceptor.CanIntercept(type))
        {
            return;
        }

        IInterceptionBehaviorsPolicy behaviorPolicy = FindInterceptionPolicy<IInterceptionBehaviorsPolicy>(context);

        IEnumerable<IInterceptionBehavior> interceptionBehaviors = behaviorPolicy == null
                                                                       ? Enumerable.Empty<IInterceptionBehavior>()
                                                                       : behaviorPolicy.GetEffectiveBehaviors(context, interceptor, type, type).Where(ib => ib.WillExecute);

        IAdditionalInterfacesPolicy interceptionPolicy3 = FindInterceptionPolicy<IAdditionalInterfacesPolicy>(context);
        IEnumerable<Type> additionalInterfaces1 = interceptionPolicy3 != null ? interceptionPolicy3.AdditionalInterfaces : Type.EmptyTypes;
        context.Policies.Set(new CustomEffectiveInterceptionBehaviorsPolicy() { Behaviors = interceptionBehaviors }, context.BuildKey);

        Type[] additionalInterfaces2 = Intercept.GetAllAdditionalInterfaces(interceptionBehaviors, additionalInterfaces1);
        Type proxyType = interceptor.CreateProxyType(type, additionalInterfaces2);

        IPolicyList containingPolicyList;
        IConstructorSelectorPolicy originalConstructorSelectorPolicy = context.Policies.Get<IConstructorSelectorPolicy>(context.BuildKey, out containingPolicyList);

        if (originalConstructorSelectorPolicy is DefaultUnityConstructorSelectorPolicy)
        {
            containingPolicyList.Set<IConstructorSelectorPolicy>(new CustomDerivedTypeConstructorSelectorPolicy(proxyType, originalConstructorSelectorPolicy), context.BuildKey);
        }
    }

    public override void PostBuildUp(IBuilderContext context)
    {
        Guard.ArgumentNotNull(context, "context");

        IInterceptingProxy interceptingProxy = context.Existing as IInterceptingProxy;

        if (interceptingProxy == null)
        {
            return;
        }

        CustomEffectiveInterceptionBehaviorsPolicy interceptionBehaviorsPolicy = context.Policies.Get<CustomEffectiveInterceptionBehaviorsPolicy>(context.BuildKey, true);

        if (interceptionBehaviorsPolicy == null)
        {
            return;
        }

        foreach (IInterceptionBehavior interceptor in interceptionBehaviorsPolicy.Behaviors)
        {
            interceptingProxy.AddInterceptionBehavior(interceptor);
        }
    }

    private static TPolicy FindInterceptionPolicy<TPolicy>(IBuilderContext context) where TPolicy : class, IBuilderPolicy
    {
        TPolicy policy = context.Policies.Get<TPolicy>(context.BuildKey, false);

        if (policy != null)
        {
            return policy;
        }

        return context.Policies.Get<TPolicy>(context.BuildKey.Type, false);
    }

    private class CustomEffectiveInterceptionBehaviorsPolicy : IBuilderPolicy
    {
        public CustomEffectiveInterceptionBehaviorsPolicy()
        {
            this.Behaviors = new List<IInterceptionBehavior>();
        }

        public IEnumerable<IInterceptionBehavior> Behaviors { get; set; }
    }

    private class CustomDerivedTypeConstructorSelectorPolicy : IConstructorSelectorPolicy
    {
        private readonly Type interceptingType;
        private readonly IConstructorSelectorPolicy originalConstructorSelectorPolicy;

        public CustomDerivedTypeConstructorSelectorPolicy(Type interceptingType, IConstructorSelectorPolicy originalConstructorSelectorPolicy)
        {
            this.interceptingType = interceptingType;
            this.originalConstructorSelectorPolicy = originalConstructorSelectorPolicy;
        }

        public SelectedConstructor SelectConstructor(IBuilderContext context, IPolicyList resolverPolicyDestination)
        {
            return FindNewConstructor(this.originalConstructorSelectorPolicy.SelectConstructor(context, resolverPolicyDestination), this.interceptingType);
        }

        private static SelectedConstructor FindNewConstructor(SelectedConstructor originalConstructor, Type interceptingType)
        {
            ParameterInfo[] parameters = originalConstructor.Constructor.GetParameters();
            SelectedConstructor selectedConstructor = new SelectedConstructor(interceptingType.GetConstructor(parameters.Select(pi => pi.ParameterType).ToArray()));

            foreach (string newKey in originalConstructor.GetParameterKeys())
            {
                selectedConstructor.AddParameterKey(newKey);
            }

            return selectedConstructor;
        }
    }
}

public class CustomInterception : Interception
{
    protected override void Initialize()
    {
        this.Context.Strategies.AddNew<InstanceInterceptionStrategy>(UnityBuildStage.Setup);
        this.Context.Strategies.AddNew<CustomTypeInterceptionStrategy>(UnityBuildStage.PreCreation);
        this.Context.Container.RegisterInstance(typeof(AttributeDrivenPolicy).AssemblyQualifiedName, (InjectionPolicy)new AttributeDrivenPolicy());
    }
}