Skip to content

Newtonsoft JSON uses less ram then System.Text.Json on same large JSON #115816

Open
@vasicvuk

Description

@vasicvuk

Description

We have a scenario to load a large JSON file into .NET but noticed that the memory usage went high when using System.Text, we also tried to use Newtonsoft.JSON then and it shows way less memory usage for the same 200mb file JSON.

Code for Newtonsoft JSON:

DefaultContractResolver contractResolver = new DefaultContractResolver
{
    NamingStrategy = new CamelCaseNamingStrategy()
};
using StreamReader r = new(stream);
using JsonReader readerData = new JsonTextReader(r);
Newtonsoft.Json.JsonSerializer serializer = new()
{
    ContractResolver = contractResolver,
};
serializer.Converters.Add(new StringEnumConverter());
return serializer.Deserialize<ExecutionModelDTO>(readerData);

Image

Code for System.Text.Json:

var options = new JsonSerializerOptions
{
    PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
    UnmappedMemberHandling = System.Text.Json.Serialization.JsonUnmappedMemberHandling.Skip,
    Converters =
{
    new JsonStringEnumMemberConverter(KebabCaseNamingPolicy),
    }
};
return  await System.Text.Json.JsonSerializer.DeserializeAsync<ExecutionModelDTO>(stream, options)
    .ConfigureAwait(false);

Image

We tried also using Deserialize with ReadToEnd but this causes ram to go up to 1.2gb usage and it is not released.

Configuration

Dotnet: 8.0.116
Both on Windows and Linux Container

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions