Skip to content

Use of ReferenceHandler.Preserve (considerably) increase Memory Pressure #47569

@codingdna2

Description

@codingdna2

Description

I've enabled in my ASP.NET Core project the usage of the built-in ReferenceHandler as following.

services.AddControllers()
    .AddJsonOptions(options =>
    {
        options.JsonSerializerOptions.IgnoreNullValues = true;
        options.JsonSerializerOptions.Converters.Add(new TimeSpanConverter());
        options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve;
    });

I've observed my process, which was previously running with a memory consuption of 110-120Mb, running at over 2GB (and increasing) a few seconds minutes after I've connected my client. My client is a C# client generated with NSwag.

Configuration

  • NET 5.0
  • Windows 10 v20H2 build 19042.746
  • Architecture x64

Other information

I've tried to have a look at source code and I see PreserveReferenceHandler have a static reference (ReferenceHandler.cs) and contains a dictionary from which values are never removed (PreserveReferenceResolver.cs).. So I can just guess disposing would occur when the whole serializer is disposed but it's not clear to me when this happens.

Am I using it in a wrong way?
Thanks in advance for any help.
Dan

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions