Open
Description
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);
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);
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