Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hashtable - out of memory

I am using Hashtable in my c# application. I am loading millions of key into, but after the application exceed the 3,7GB of RAM it gives me an "out of memory" exception.

I use x64 operation system, and the computer has 16GB of ram. I was thinking about maybe this could be an x86 limitation. I changed the build type to x64 but I still get the error.

Is there a maximum memory size for an object in .net? Can I do something to use all the memory?

Thanks, Andrew

like image 866
Andrew Avatar asked Dec 03 '22 13:12

Andrew


2 Answers

Take a look on this previous answer: .NET Max Memory Use 2GB even for x64 Assemblies

The 2GB limit applies to each object individually. The total memory used for all objects can exceed 2GB.

like image 187
Rubens Farias Avatar answered Dec 20 '22 21:12

Rubens Farias


Use a Dictionary<,> instead of HashTable. In a HashTable both the key and value are objects, so if they are value types they will be boxed. A Dictionary can have value types as key and/or value, which uses less memory. If you for example use an int as key, each will use 28 bytes in a HashTable while they only use 4 bytes in a Dictionary.

If both the key and value are value types and use less than 8 bytes the Dictionary will be able to hold more items than the HashTable.

like image 40
Guffa Avatar answered Dec 20 '22 21:12

Guffa