-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Memory leaking with Serializing JSON on Large Objects #75314
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue Detailsnow there is Memory leaks with
|
Is it actually a leak? Has the GC run? If you look at the objects still alive, where are they rooted? My guess is these are large arrays in the ArrayPool, and there simply hasn't been enough memory pressure and/or GCs to cause it to release them all. |
Working set doesn't mean there's a leak. In a managed environment, the GC gets to decide when to decommit memory. I'd recommend taking a memory profile: |
Couple of questions:
Thanks! |
This issue has been marked |
1- No, it's .NET 7.0.0-rc.2.22451.11 |
The title of that view is missing, but it appears to be the stack that allocated the objects. What's more interesting is the path to root that's keeping the objects alive. |
Is it related to the same problem or not? |
Do you need any more information??? |
I'm not yet convinced there's a problem.
Yes, what I asked for above: Alternatively if you have a dump of the process to share that's fine, too. |
I known that but I need ignore all and add attribute like [Jsonspecific] to serialize that. |
Maybe you should write a custom JSON converter. It looks like we don't have any memory leak here though, so the issue is resolve right? |
But why duplicates when I serialize it and still alive even I don't use it or create it? |
I will test serialize with Newtonsoft JSON.NET if the same case I'll use custom converter. |
The serializer uses pooled memory so it’s allocated and reused for future calls. As for your specific situation, I’m still not clear on what the leak is… Maybe providing a runnable sample would be the next step here. |
Right, this is what I was expecting we'd see. |
why newtonsoft reverse 400 MB and system.text.json reverses 1.6 GB? After serializing |
https://github.com/AhmedZero/ReactiveObjectSerializeTest |
Most of the memory seems to be coming from the fact that you're deriving from this ReactiveObject type. I'm not sure if that's just a convenience but the entire object graph is being serialized that that's not what you want. There's no leak here. I'm pretty sure the difference between this an JSON.NET is the fact that it respects |
I told you that. |
I told you how to ignore them, but you don't own ReactiveObject so the attributes won't do you any good. Write a custom JSON converter and be done with it 😄. |
I already write a custom JSON converter, but I hope to support it soon like JSON.NET |
Maybe there's some clever way to use JsonInclude that I'm not aware of to solve this problem. |
There's not much that can be done with base types in third-party libraries using attributes. Starting with .NET 7 it should be possible to influence what properties do get serialized using contract customization, which removes the need (and pitfalls) of writing a custom converter. Here's what a simplistic variant of such a contract resolver could look like: var options = new JsonSerializerOptions
{
TypeInfoResolver = new DefaultJsonTypeInfoResolver()
{
Modifiers =
{
static jsonTypeInfo =>
{
if (typeof(ReactiveObject).IsAssignableFrom(jsonTypeInfo.Type))
{
IList<JsonPropertyInfo> properties = jsonTypeInfo.Properties;
for (int i = 0; i < properties.Count; i++)
{
if (properties[i].PropertyType.IsGenericType &&
properties[i].PropertyType.GetGenericTypeDefinition() == typeof(IObservable<>))
{
jsonTypeInfo.Properties.RemoveAt(i--);
}
}
}
}
}
}
}; Using that contract resolver in your test project resulted in memory consumption dropping substantially. |
it's better than JSON.NET now. |
now there is Memory leaks with
JsonSerializer.Serialize
when i convert list of objects, the memory increases from 950 MB to 2.5 GB.before
JsonSerializer.Serialize
after
JsonSerializer.Serialize
i test with .NET 7.0.0-rc.2.22451.11
my OS is Windows 11
the app is X86
The text was updated successfully, but these errors were encountered: