I have a UWP media app which is unable to stay below the 128MB for Xbox One running in background mode. Using the memory profiler, it seems that JSON.Net is utilizing the most resources both before deserialization has even started and after deserialization has finished and garbage collection has been forced.
Based on everything I've read, I'm instantiating a local DefaultContractResolver, which should not cache resources. I'm also deserializing from a stream, which according to the JSON.Net "Performance Tips" is the most memory efficient way to process json. I'm at a loss on why so many resources are still being held on to after garbage collection.
Before deserialization:
After deserialization and forced garbage collection:
Deserialization code:
public class JsonExtensions
{
public T DeserializeStringNoCache<T>(string json, JsonSerializerSettings settings = null)
{
using (Stream s = StringHelper.GenerateStreamFromString(json))
using (StreamReader sr = new StreamReader(s))
using (JsonReader reader = new JsonTextReader(sr))
{
JsonSerializer serializer = new JsonSerializer();
serializer.ContractResolver = new DefaultContractResolver();
return serializer.Deserialize<T>(reader);
}
}
}