1

I'm using Newtonsoft.Json lib for deserializing several JSON. I'm re-using the default contract resolver and I'm aware that it caches the types for future uses.

Here's a print from the memory issue.

My problem is that these types will never be the same (I'm always changing the json used), since my program runs indefinitely my memory keeps raising over time. Is there a way to clear this cached Contract Resolvers from time to time?

UPDATE 1

I tried @dbc answer and the memory raise from the ThreadSafeStore is not raising anymore: dbc answer , but the ConcurrentDictionary's keeps on raising,is there anything to fix this? ConcurrentDictionary's

  • If you are using `DefaultContractResolver` then if you simply remove all references to your resolver instance, most (not all) memory will be reclaimed. But if you are using `CamelCasePropertyNamesContractResolver` all memory is shared globally, so don't do that. See: [Does Json.NET cache types' serialization information?](https://stackoverflow.com/a/33558665/3744182). – dbc May 05 '20 at 19:44
  • @dbc i just saw the topic you mentioned, but i don't know to apply that on my project, or how to remove all the references from the resolver instance. Can you help me? – Lucio Zenir May 05 '20 at 19:52
  • I edited my answer there to simplify it a little. Basically you remove all references to a resolver (or any .Net object) simply by not retaining any. This is most easily done by allocating the object within a single method and not returning it out. An example is shown in the answer. – dbc May 05 '20 at 20:07
  • @dbc i just tried your first answer and the ThreadSafeStore is not caching memory anymore, but i still keep getting memory raise on the ConcurrentDictionary's, can you help me with that?, i will edit my post with a print – Lucio Zenir May 05 '20 at 20:21
  • That's probably something that is cached globally. As mentioned in the previous answer, some reflection information is cached globally rather than within each resolver. – dbc May 05 '20 at 21:23
  • 1
    Is there a way to clear the global cached information? since my program never stops running i need a way to release that memory. – Lucio Zenir May 06 '20 at 12:13

0 Answers0