JamesNK / Newtonsoft.Json

Json.NET is a popular high-performance JSON framework for .NET
https://www.newtonsoft.com/json
MIT License
10.77k stars 3.25k forks source link

It seems a memory leak when using Newtonsoft.Json for dotnet core. #2054

Open dickence opened 5 years ago

dickence commented 5 years ago

I have published a dotnet core webapi project to IIS. But the memory is increasing fast. I checked the memory by diagnostic tools in Visual Studio. This is the result:

屏幕快照 2019-04-22 22 09 21

.net core SDK version: V2.0.5 newtonsoft.json version: V11.0.2 & V12.0.2

The code is:

            var pageList = new PageDevTemp()
            {
                recordsTotal = nCount,
                data = retlist,              // type of retlist is List<dynamic>
                recordsFiltered = tempList.Count
            };
            return JsonConvert.SerializeObject(pageList);

I called the same api twice with the same parameters, the results of memory increasement are similar. It seems that memory about newtonsoft.json is increasing fastest, and never goes down. Is it a bug? Or do I use newtonsoft.json in a wrong way?

fschlaef commented 5 years ago

I have the exact same issue, but it doesn't seem that the problem lies within Newtonsoft.Json, but rather how the .Net Core runtime handles large objects in your case, probably retlist.

Many objects stay in memory with no way to release them until the process memory passes a threshold.

I would say nothing to worry here, unless you get OutOfMemoryException.

joetherod commented 5 years ago

nothing to worry here? If its a memory leak issue in core, it should be resolved. Seems that core has very poor memory management. I ported over an existing mvc app to core and have these ' memory issues' that dont seem to get resolved.

dcoen commented 4 years ago

We are having this issue too. We are in .NET Core as well.

I suspect it is from the StringWriter used in SerializeObjectInternal NOT being disposed. That implements IDisposable and should be placed in a using() block.

image image image

dcoen commented 4 years ago

You all can probably ignore my earlier comment....i think this instead an issue with Microsoft's Durable Task Framework trying to serialize an Exception (presumably with a self referencing loop) using Newtonsoft. Specifically, DurableTask.Core.Serializing.JsonDataConverter.

An the exception is coming from AutoMapper, and i think this is that issue: https://github.com/JamesNK/Newtonsoft.Json/issues/1798

AlexanderKot commented 2 years ago

Hello Seems having the same problem. .net core SDK version: V6.0.7 newtonsoft.json version: V13.0.1

I also trying serialize object containing inside List < dynamic >, and this can be source of problem. doing this - have many leaked object related too newtonsoft.

For example 11

I have tried using separate instance of DefaultContractResolver per such call - it do not help screenshot exactly with such usage. (but in it source i do not see any statics to be used as root for leak)

using CamelCasePropertyNamesContractResolver seems even worse - it has embedded place for leak (_contractCache) +1 entry after each call of such obj serialization

Seems it is really Json.NET problem not .net core Seems it creates internal "contracts" based on reflection for dynamic each time Is there any possibility for not caching such contracts?

AlexanderKot commented 2 years ago

Further investigation shows that it was bug in our code :) In this case List < dynamic > contains new dynamically created type each call and JN creates internal structures for serializing this types....

So it is fixed on our app side.

But small problem in JN really exists Why it do internal caching of these structures when new instances of contract resolver used each time? What to do in situation when developer have no influence and what needed to be serialized, but understood that it is temporary existing type?

uugan commented 1 year ago

Any updates on this? I'm also having memory leakage by using this library on asp.net core 3.1 with newtonsoft.json 13.0.1. JsonConvert.SerializeObject JObject.Parse It seems these 2 functions are having problems. From controller we're adding to queue and 2 consumers are peeking from queue and notifies to blocked object for returning response. Objects are increasing, and garbage collector doesn't work to clean these objects. image

After each http call I took snapshots: image

justintien commented 1 year ago

It seems these 2 functions are having problems. From controller we're adding to queue and 2 consumers are peeking from queue and notifies to blocked object for returning response. Objects are increasing, and garbage collector doesn't work to clean these objects.

I use newtonsoft.json 13.0.1 in unity.

I also had a memory leak

I tested, even new JObject(), or JObject.parse, all the memory leak.

Has there been any progress so far?

mynkow commented 1 year ago

I also have this issue as well. There is a repro here

This really hurts. In my case it is just a normal collection being deserialized.