0

We are maintaining an in-process, in-memory cache of messages, shared across threads. A message is a tree of JToken (usually a JObject with lots of properties and subproperties).

We want to keep track of the memory usage of this cache, as realistically as possible.

Right now, we calculate the memory cost of a message by calculating the number of bytes it takes once serialized to BSON, but this is likely to be quite far off the actual memory usage of a tree of small objects in the .NET VM.

Is there a better way to track the memory consumption of our cache?

François Beaune
  • 4,270
  • 7
  • 41
  • 65
  • You'd better not to use `JObject` for caching. I loaded a 500-600 MB file to memory as `JObject`, and it took about 10 GB of memory. Finally, we moved from `JObject` to `dynamic`(ExpandoObject). It consumes 3-5 times less memory than `JObject`. For the same file it took only 2GB of memory. – Jury Soldatenkov Nov 18 '16 at 10:16
  • @JurySoldatenkov That's an interesting observation. Not sure why using `dynamic` would reduce memory usage though. Any idea? In any case, if we can afford it, we'd like to stick to caching pre-parsed messages, i.e. caching them as trees of `JToken`. – François Beaune Nov 18 '16 at 10:49
  • it's because `JToken` based classes contain multitude of metadata fields. It envelops every detail in a JToken based class. You can [check it yourself](https://github.com/JamesNK/Newtonsoft.Json/tree/master/Src/Newtonsoft.Json/Linq). In fact, `dynamic` is [`ExpandoObject`](https://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject(v=vs.110).aspx) which is very similar to dictionary internally. It doesn't have redundant envelopes and metadata. – Jury Soldatenkov Nov 18 '16 at 13:34
  • http://stackoverflow.com/a/35528813/17034 – Hans Passant Nov 18 '16 at 14:42
  • Interesting, thanks for the tip! I'll investigate to check if switching to `dynamic` helps in our case, but based on your analysis, it should. It also shows how far from the real memory usage we must be right now... – François Beaune Nov 18 '16 at 15:03

0 Answers0