Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dictionary vs Hashtable memory usage

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?

like image 228
pablo Avatar asked Jul 18 '11 22:07

pablo


1 Answers

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.

like image 165
Darin Dimitrov Avatar answered Oct 17 '22 06:10

Darin Dimitrov