views:

286

answers:

3

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

+5  A: 

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.

Rubens Farias
+1, beat me while I was still typing. See also: http://blogs.msdn.com/ericlippert/archive/2009/06/08/out-of-memory-does-not-refer-to-physical-memory.aspx
Sapph
@Sapph, ty for that link, I was looking for it to complement this answer =)
Rubens Farias
+1  A: 

This article states that .NET limits the size of a single object to two gigabyte.

Daniel Brückner
+3  A: 

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.

Guffa
+1, very precise information Guffa, ty
Rubens Farias