Search code examples
c++jsonnlohmann-json

How to get size of nlohmann json object in memory?


How do I get the size of nlohmann object in memory in bytes? The .size() operator gives the number of elements. I have a lot of sub keys and elements. What can I do to find it?


Solution

  • Technically, you'd get this by using a custom allocator. The NLohmann JSON library supports custom allocators, which replace std::allocator. You can provide one that counts the various allocations. Note that the support is a bit flawed.

    But as the comments suggest, you want a smaller representation. The working copy in memory is typically bigger, fragmented, and even if you collected all the fragments there's little you can do with them.