C#中Dictionary的内存使用情况 [英] Memory usage of Dictionary in c#

查看:416
本文介绍了C#中Dictionary的内存使用情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c#中有一个带有字典的程序,我这样定义它:

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

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

然后,我在字典中添加了100条条目.现在,我想知道这本词典的确切内存使用情况. 请不要谈论sizeof以及类似的方法,我想要一种数学方法来计算它,例如,如果应该保存任何指针或任何哈希键,请让我知道(实际上我不知道字典工作的确切方式).

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

推荐答案

这是该类型的内部实现细节;它不会在外部公开足够的信息来供您计算其所有成员的大小(假设您对实例的大小不感兴趣,也对它拥有的对象的大小不感兴趣).

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.

这篇关于C#中Dictionary的内存使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆