Skip to content

Memory continues to grow even when clearing instance cache #83

@wegnerb

Description

@wegnerb

We are currently using version 2.0.4 of Slapper.AutoMapper and ultimately we love the tool for what it provides. We discovered while pulling 50,000 complex'ish objects from our database that the memory continued to grow and never seemed to recover. Our application is a .NET Core 3.1 Console Application running as a HostedService.

Below is the code that we have been executing. Some values are changed to protect the innocent but I think you get the idea. We are passing in false to the MapDynamic method that should ultimately call the static ClearInstanceCache method.

DynamicParameters parameters = new DynamicParameters();
parameters.Add("@id", id);
parameters.Add("@numberOfRows", numberOfRows);

IEnumerable<dynamic> result = null;

result = _unitOfWork.Connection.Query<dynamic>("dbo.ssp_byid", parameters, _unitOfWork.Transaction, commandType: CommandType.StoredProcedure, commandTimeout: 0);

Slapper.AutoMapper.Configuration.AddIdentifier(typeof(CustomObject), "Id");
IEnumerable<CustomObject> objects = Slapper.AutoMapper.MapDynamic<CustomObject>(result, false);

return objects?.ToList();

This is what we observe happening within the Diagnostics Tools while pulling the 50,000 records

image

and then we dug in to the memory heap a bit and found that there seemed to be a lot of objects stuck in a Dictionary that seemed a lot like the dictionary related to the instance cache

image

I then pulled down the latest code from the repository and hooked our code up to it locally so I could walk through and see what was happening. What I discovered was that even though the instance cache dictionary was removed from the ContextStorage all of the elements of that dictionary seemed to remain in memory and weren't recovered by the garbage collector. Here is the code in question

public static void ClearInstanceCache()
{
    InternalHelpers.ContextStorage.Remove(InstanceCacheContextStorageKey);
}

I found that if I changed the code to something like this the memory stayed consistent while running our process

public static void ClearInstanceCache()
{
    var instanceCache = InternalHelpers.ContextStorage.Get<Dictionary<InternalHelpers.InstanceKey, object>>(InstanceCacheContextStorageKey);
    if (instanceCache != null)
    {
        instanceCache.Clear();
        InternalHelpers.ContextStorage.Remove(InstanceCacheContextStorageKey);
    }
}

I was wondering if we could get this fix in to a future release. I would have been happy to put this in to a pull request but to be 100% transparent I'm not exactly sure how to do that within a repo hosted in github

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions