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?