Search code examples
memory-leaksautomapperc#-6.0asp.net-core-6.0

AutoMapper Memory Leak


AutoMapper is taking too much memory. I think it's a memory leak.

I'm using 11.0.1 version in .Net Core 6 framework.

I've profiled the project. Here are a couple of screenshots: enter image description here

Memory Usage reaches up to 2 GB by the Web API

Memory Usage reaches up to 2 GB by the Web API

Any ideas as to why it is taking this much memory for around 400 mapping classes?

Here's the configuration of AutoMapper being applied from the static IServiceCollection class:

    services.AddAutoMapper(
            cfg => cfg.AddMaps("WebApi.Common"),
            typeof(MapperProfile),
            typeof(VendorProfile));


Solution

  • I fixed it by configuring AutoMapper according to the latest documentation.

    Below is the code that fixed this issue:

    var mapperConfig = new MapperConfiguration(mc =>
    {
        mc.AddMaps("WebApi.Common");
        mc.AddMaps("WebApi.Core");
    });
    
    IMapper mapper = mapperConfig.CreateMapper();
    services.AddSingleton(mapper);
    

    Previously, it was configured according to the documentation of an older version of AutoMapper.