Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OutOfMemoryException when adding more items to a very large HashSet<Int32>

Exception of type System.OutOfMemoryException was thrown while trying to add 23997908th item in a HashSet<Int32>.

We need maintain a high performance unique collection of integer sizeof Int32.MaxValue i.e. 2147483647. HashSet of Int32 can store only 23997907 items in it. Looking for a suggestion to resolve this issue.

like image 305
Debasis Avatar asked Dec 27 '11 06:12

Debasis


People also ask

How do you fix system OutOfMemoryException exception of type system OutOfMemoryException was thrown?

OutOfMemoryException Exception of type 'System. OutOfMemoryException' was thrown. To resolve this issue, I had to restart Visual Studio or go to the Windows Task Manager and terminate IIS Express process. This error could happen due to a variety of reasons related to memory consumption of the application.

What causes OutOfMemoryException?

When data structures or data sets that reside in memory become so large that the common language runtime is unable to allocate enough contiguous memory for them, an OutOfMemoryException exception results.

How can avoid out of memory error in C#?

Well, according to the topic of the question, best way to avoid out of memory exception would be not to create objects that fill in that memory. Then you can calculate the length of your queue based on estimate of one object memory capacity. Another way would be to check for memory size in each worker thread.


2 Answers

capacity of a HashSet(Of T) object is the number of elements that the object can hold. object's capacity automatically increases as elements are added to it.

if you are using 64 bit system, you can increase Hashset's max capacity upto 2 billion elements by setting the enabled attribute of the gcAllowVeryLargeObjects to true in runtime environment.

You can enable this settings from config file,

<configuration>
 <runtime>
   <gcAllowVeryLargeObjects enabled="true" />
  </runtime>
 </configuration>

Check this MSDN link for setting the configuration.

Update:

Above config gcAllowVeryLargeObjects supports on .Net framework 4.5 only.

like image 187
mehul9595 Avatar answered Nov 15 '22 14:11

mehul9595


HashSet grows by doubling. So when you have 23,997,907 items in the list and try to add the next one, it tries to double the size of its backing array. And that allocation causes it to exceed available memory. I assume you're running this on a 32-bit system, because on a 64-bit system a HashSet<object> can hold upwards of 89 million items. The limit is about 61.7 million items in the 32-bit runtime.

What you need to do is pre-allocate the HashSet to hold as many items as you need. Unfortunately, there's no direct way to do that. HashSet doesn't have a constructor that will pre-allocate it with a given capacity.

You can, however, create a List, use it to initialize the HashSet, and then call Clear on the HashSet. That ends up giving you a HashSet that has no items in it, but a capacity of the max you requested. I showed how to do that in a blog post: More on .NET Collection Sizes.

The limits on HashSet size are due to the two gigabyte limit in .NET. No single object can be larger than two gigabytes. The number is actually slightly smaller, due to allocation overhead.

like image 30
Jim Mischel Avatar answered Nov 15 '22 16:11

Jim Mischel