Memory usage of Dictionary in c# [duplicate]

南笙酒味 提交于 2020-01-17 11:14:08

问题


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).


回答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.



来源:https://stackoverflow.com/questions/20430157/memory-usage-of-dictionary-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!