0

Two cases in particular:

First: big.Int

regardless of the number stored, unsafe.Sizeof returns 16. It obviously doesn't count the data that represents the number, just a pointer or reference to it. Is there a call I can make to account for all the memory being used by the big.Int?

Second: linked list

If I have a struct which includes pointers to things which have pointers and so on. Obviously unsafe.Sizeof only returns the size of the member pointer, not the things it points to and certainly not recursively.

Are there easy ways of accessing how much memory such things are using in total?

https://play.golang.org/p/bGbQ_4pZP7W

user40176
  • 319
  • 2
  • 10
  • You may use the benchmarking tool to get the size of any variable or structure. See the marked duplicate. – icza Feb 09 '19 at 10:03

1 Answers1

0

No, there is no "magic" or easy way to do this. The only option is to recurse the structure yourself. And even then, it's often a matter of definition what counts toward memory usage. As an example, you'd almost certainly not want to count circular references more than once.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189