Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8958745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:17:07+00:00 2026-06-15T15:17:07+00:00

So we’re running into a problem where the container is holding onto instances of

  • 0

So we’re running into a problem where the container is holding onto instances of DerivedTypeConstructorSelectorPolicy for the duration of the application. This wouldn’t be so bad were it not for the fact that the following two lines of code in TypeInterceptionStrategy wraps the old policy into a new one, which then now holds onto two instances (or more, every call to Resolve for an intercepted class will compound the problem). You can see this when running .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);

This is causing thrashing of the Gen 2 collection when you have a lot of classes being intercepted and then you get holes in memory that the GC cannot reclaim, especially as policies get copied to ensure thread safety. Once you have a sufficient number of items in the policy list, it will get moved to the LOH, which makes the thrashing even worse.

I should caveat this with the fact that our application is running in IIS Classic and not Integrated Pipeline and in 32-bit mode, double whammy, I know. With more addressable space in 64-bit mode, this wouldn’t be so bad. But still, we’re stuck with the virtual memory that is given to us by IIS and this goes quickly as the GC can’t find enough space in the VM map to allocate additional memory and requests start dying.

The file only has 3 revisions, with the original revision looking like it did not have this issue. Has anyone else run into this problem or am I missing something that perhaps explains this behavior?

A link to a very simple program that illustrates this is posted on pastebin: http://pastebin.com/DYG3GXNm

Using .NET Memory Profiler (or your profiler of choice) and watching for the DerivedTypeConstructorSelectorPolicy, you can watch it grow through each iteration and as it grows, you can examine and see the originalConstructorSelectorPolicy keeps referencing the old instance in a long chain.

As an example of how many classes we intercept, it’s on the order of about 1300 or so registrations.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:17:08+00:00Added an answer on June 15, 2026 at 3:17 pm

    I’ve found a solution in the meantime. It’s a simple fix, but it requires that you override the Interception and TypeInterceptionStrategy. The fix was a one-liner that simply involved checking what type was coming out the policy list.

    This is code from the TypeInterceptionStrategy:

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

    Of course, in order to change that, you have to copy the TypeInterceptionStrategy and do the same things it does with just that fix, it can’t be fixed simply from override the PreBuildUp method.

    I’m pasting the entire fix here in case others run into the problem.

    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());
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently running into a problem where an element is coming back from
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.