-1

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?

Sreeraj Chundayil
  • 5,548
  • 3
  • 29
  • 68
  • A `nlohmann::json` object is not a trivial type, so it isn't meaningful to look at the bytes of such an object. What are you trying to do where you think you need to inspect the bytes of a `json` object? – Caleth Apr 06 '21 at 13:29
  • @Caleth: We are trying to pass a nlohmann object to another application through api. So wanted to check the size. The application has a size limit. We want to optimize if it exceeds above. – Sreeraj Chundayil Apr 06 '21 at 13:36
  • 2
    In the same way that you can't pass the *contents* of a `std::deque` by copying the *bytes* of the deque, you can't do that with a `json` object. Perhaps you should get a string representation of the JSON and transfer that – Caleth Apr 06 '21 at 13:58
  • Ok. That's what I have currently. – Sreeraj Chundayil Apr 06 '21 at 14:05
  • json.dump().size() doesn't work for you? – Joseph Larson Apr 06 '21 at 17:45
  • @JosephLarson: No, the dump() will have the braces which we need to avoid for calculating the size in memory. – Sreeraj Chundayil Apr 07 '21 at 04:24
  • @InQusitive Pretty easy to trim the braces out. I suppose it depends on the definition of size. Number of objects as you traverse the tree? Or number of bytes required to store it? – Joseph Larson Apr 08 '21 at 05:48
  • @JosephLarson Number of bytes required to store it in memory. – Sreeraj Chundayil Apr 08 '21 at 06:38
  • @InQusitive So that means you need to know how much space each std::string takes (for instance), how much overhead in each std::vector and std::map (or whatever the library is using under the hood). Very non-trivial. – Joseph Larson Apr 08 '21 at 15:56

1 Answers1

1

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.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • Instead of using json dump file, what if we pass around the json object itself? If it has very less size compared to file, then that would be beneficial to us. Can the size of json object be greater than the json.dump file itself? – Sreeraj Chundayil Apr 07 '21 at 04:26
  • 1
    @InQusitive: How would it be much smaller? There's no magic involved in computers. Sure, in memory you don't need to store the `{` and `}`, but a pointer will be larger than two characters. – MSalters Apr 08 '21 at 07:12