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

  • Home
  • SEARCH
  • 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 9002369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:33:42+00:00 2026-06-16T00:33:42+00:00

I seem to have a memory leak within the usage of AutoMapper and StructureMap

  • 0

I seem to have a memory leak within the usage of AutoMapper and StructureMap which are used within my WCF REST service.

Under load test I can see the memory usage continually going up and when I examined it more closely using a memory profiler I can see that there are many instances of an object used by the MappingEngine building up (approx 9x amount of any other instance)

Class Name=Live Instances
Object=498,847
Int32[]=69,373
Object[]=68,116
ConcurrentDictionary<TKey, TValue>+Node<TypePair, IObjectMapper>=37,624
string=35,240
IObjectMapper[]=30,782
EventHandler<TypeMapCreatedEventArgs>=30,782
MappingEngine=30,781
ConcurrentDictionary<TypePair, IObjectMapper>=30,781
ConcurrentDictionary<TKey, TValue>+Node<TypePair, LambdaExpression>[]=30,781
ConcurrentDictionary<TKey, TValue>+Node<TypePair, IObjectMapper>[]=30,781
ConcurrentDictionary<TypePair, LambdaExpression>=30,781

and here is the a typical instance retention graph…

bootstrapper._configuration
 -> AutoMapper.ConfigurationStore (TypeMapCreated)
 -> System.EventHandler<TypeMapCreatedEventArgs> (multicast delegate)
 -> System.Object[]
 -> System.EventHandler<TypeMapCreatedEventArgs>
 -> AutoMapper.MappingEngine (_objectMapperCache)
 -> System.Collections.Concurrent.ConcurrentDictionary<Internal.typePair, IObjectMapper> (m_locks)
 -> System.Object[]
 -> System.Object

(sorry for rubbish formatting but SO wouldn’t let me post the images)

Problem is that I’m not sure whether it is a problem with my code, StructureMap or AutoMapper.

Code samples…

Bootstrapper (called from Application_Start in global.asax) for automap…

    public class BootStrapper
    {
        private static readonly IConfiguration _configuration = ObjectFactory.GetInstance<IConfiguration>();

        public static void Initialize()
        {
                _configuration.AddProfile<AutoMapperProfile>();
        }

        public class AutoMapperProfile : Profile
        {
            protected override void Configure()
            {
                    BootstrapAutoMapper();
            }   
        }

        public static void BootstrapAutoMapper()
        {
            _configuration.CreateMap<In.Account, Out.Accounts.Account>()
               .ForMember(m => m.AccountNumber, o => o.MapFrom(s => s.AcctId))
               .ForMember(m => m.Balances, o => o.Ignore())
               .ForMember(m => m.AccountTypeDescription, o => o.MapFrom(s => s.TypeName))
               .ForMember(m => m.AccountType, o => o.MapFrom(s => s.Type))
               .ForMember(m => m.IsNoticeAccount, o => o.ResolveUsing(s => IsNoticeAccountMapper.Map(s.Type, ObjectFactory.GetInstance<IAccountTypeHelper>())));

             // many other mappings excluded for brevity
    }        
}   

which is configured within StructureMap with…

    private void AutoMapperRegistration()
    {
        For<ConfigurationStore>().Singleton().Use<ConfigurationStore>().Ctor<IEnumerable<IObjectMapper>>().Is(AutoMapper.Mappers.MapperRegistry.AllMappers());
        For<IConfigurationProvider>().Use(ctx => ctx.GetInstance<ConfigurationStore>());
        For<IConfiguration>().Use(ctx => ctx.GetInstance<ConfigurationStore>());
        For<ITypeMapFactory>().Use<TypeMapFactory>();
        For<IMappingEngine>().Use<MappingEngine>();

        Scan(scan =>
             {
                 scan.AssemblyContainingType<AppRegistry>();
                 scan.AddAllTypesOf<Profile>();
             });
}

Typical WCF Rest service usage…

public class AccountService : IAccountService
{
        private readonly IMappingEngine _mappingEngine;
        private readonly IAccountFacade _accountFacade;

        public AccountService(IMappingEngine mappingEngine, IAccountFacade accountFacade)
        {
            _mappingEngine = mappingEngine;
        _accountFacade = accountFacade;
        }

        public IEnumerable<Account> GetAccounts()
        {
            var ret = new List<Account>();
            var entities = _accountFacade.GetAccounts().ToList();   
            foreach (var account in entities.Select(entity => _mappingEngine.Map<In.Account, Out.Account>(entity)))
            {
                ret.Add(account);
            }
            return ret;     
        }
}
  • 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-16T00:33:43+00:00Added an answer on June 16, 2026 at 12:33 am

    It seems the problem was caused by the configuration of the MappingEngine – it needed to be set as a singleton otherwise instances were not cleared because it is IDisposable.

    So config changed to..

    For<IMappingEngine>().Singleton().Use<MappingEngine>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I seem to have a memory leak and I'm not sure how to fix.
My application has a memory leak resulting from my usage of JDBC. I have
I seem to have a memory corruption in my C program. I used _ASSERTE(
I suspect we have a major memory leak in our ActiveMQ connection bridge -
I have found a similar issue: NSMutableArray addObject in for loop - memory leak
I seem to always have a lot of trouble with managing memory with CoreGraphics
I have a memory management problem that i can't seem to find a solution
I seem to have a fundamental gap in my memory management understanding. The code
Alright, so I think my program might have a memory leak. It's an SDL
I have been fighting a memory leak, well a little more then one it

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.