0

I have a program in c# that has a dictionary, I defined it like this:

Dictionary dict<Int32,Int32> = new Dictionary<Int32,Int32>();

Then I added 100 entry to my dictionary. Now I want to know the exactly memory usage of this dictionary. please do not talk about sizeof and some methods like this, I want a mathematical way to compute it, for example if there is any pointer or any hash key that should be saved, please let me know (actually I do not know the exact way that dictionary works).

Chavoosh
  • 425
  • 4
  • 15
  • Basically, there is no way to calculate the exact size. This link has a couple of good suggestions for estimating your memory allocation: http://social.msdn.microsoft.com/Forums/vstudio/en-US/b871dee4-6eb5-4dca-be79-b9589a79f5e9/calculate-size-of-an-object-in-net?forum=clr – paulsm4 Dec 06 '13 at 17:42
  • 1
    It's non-deterministic. Dictionary creates a list to hold all keys that have the same hashcode. You could have one list for each hashcode or not depending on how many key collisions you have. – Kevin Dec 06 '13 at 17:43
  • You know the sizes of primitive types and you know the size of a reference in your environment. You could calculate it, but it's tedious work (and doesn't account for padding). Why do you want to do this exactly? – Jeroen Vannevel Dec 06 '13 at 17:44
  • Why do you need to know the exact size of the memory footprint; what are you going to do with that knowledge? – Servy Dec 06 '13 at 17:45
  • @Kami That's not really a duplicate at all, despite having a title that would imply that it is. – Servy Dec 06 '13 at 17:47
  • well I need exact memory usage because in my problem, it is so important to know the memory usage, even in Bytes! – Chavoosh Dec 06 '13 at 18:07
  • @Chavoosh That's not an answer to the question. – Servy Dec 06 '13 at 21:41
  • 1
    Check out http://stackoverflow.com/questions/2316460/mismatch-in-object-size-returned-by-sos-dll-and-in-memory-process-size (and SOS+WinDbg in general) as it maybe the magic you are looking for... For regular people - use profiler to collect information/investigate memory issues. – Alexei Levenkov Dec 07 '13 at 03:19
  • I do not know why some one give negative point before reading and perception the question. I mentioned I want "exactly" mathematical way although the mentioned thread has not any answer for it. – Chavoosh Dec 07 '13 at 08:16

1 Answers1

1

That is an internal implementation detail of that type; it does not expose enough information externally for you to compute the size of all of it's members (assuming you're interested not in the size of the instance, but also in the size of the objects it owns as well).

You could, in theory, use reflection (or even unsafe pointers) to access the private instance variables, to try to find the size of the internal buffer, as well as the other state it is holding onto, but that would need to be very tightly tied to a specific implementation of the dictionary. It wouldn't be able to support new implementations of the class with new versions of the framework, and would need to be written to handle any changes that have been made over all of the existing versions.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • +1: Note that OP explicitly does not want to use the only built in way of computing size by recursively walking the object tree and computing sizes - which leaves basically no options... – Alexei Levenkov Dec 07 '13 at 03:18