字典与哈希表的内存使用情况
我在 SO 上读到,除了避免装箱/拆箱的优点之外,哈希表和字典几乎是相同的。
使用 Ants Profiler,我测量了一个非常简单的应用程序,其结构如下
class Node
{
Dictionary<string, Node> Children = new Dictionary<string, Node>();
}
:
类 NodeOld { 哈希表子级 = new Hashtable(); }
好的,第一个 150 万个实例的列表大约需要 140Mb,而第二个需要超过 700Mb(64 位系统)。
那么,实施方面存在巨大差异,不是吗?
Ants Profiler 在大型示例中展示了大量的 Hashtable+Bucket 对象...
那么,如果您必须坚持使用 1.1,是否有一个等效的(内存敏感的)字典选项?
I've read here at SO that Hashtable and Dictionary are pretty much the same except for the advantages of avoiding boxing/unboxing.
Using the Ants Profiler I measure a very simple app with the following structures:
class Node
{
Dictionary<string, Node> Children = new Dictionary<string, Node>();
}
and
class NodeOld { Hashtable Children = new Hashtable(); }
Ok, a list of 1.5Million instances of the first takes about 140Mb, while the second needs more than 700Mb (64bits system).
So, there's a HUGE difference in implementation, isn't it?
The Ants Profiler unveils a HUGE number of Hashtable+Bucket objects on the big-sized example...
So, is there an equivalent (memory-savvy) option for Dictionaries if you've to stick to 1.1?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
即使我停留在 .NET 1.1 上,我也不会在内存中存储 150 万个实例,所以我不会关心。就内存消耗和速度而言,哈希表可能是在 .NET 1.1 中实现哈希表的最佳数据结构。当然,如果您更详细地解释了您的场景,并且您已经确定哈希表实际上是您的应用程序的瓶颈,那么可能会有一些更好的解决方案。
Even if I am stuck on .NET 1.1 I wouldn't be storing 1.5 million instances into memory, so I wouldn't care about. Hashtable is probably the best data structure implementing a hash-table you could get in terms of memory consumption and speed in .NET 1.1. Of course if you explained your scenario in a more details and that you have identified that Hashtable is actually a bottleneck for your application there might be some better solutions.