Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IndexOutOfRangeException when adding to Hashset<T>

Tags:

I have a simple application that adds about 7 million short strings to a HashSet<string>. Occasionally I get an exception during a call to Hashset.Add(): System.Collections.Generic.HashSet`1.IncreaseCapacity(): Index was outside the bounds of the array.

It's an intermittent problem and seems related to memory, but this is on a win2k8 R2 server with 16 GB, not much else going on, most of that physical memory is available. Any ideas?

like image 577
dcrobbins Avatar asked Nov 29 '10 19:11

dcrobbins


People also ask

When to use IndexOutOfRangeException in c#?

An IndexOutOfRangeException exception is thrown when an invalid index is used to access a member of an array or a collection, or to read or write from a particular location in a buffer. This exception inherits from the Exception class but adds no unique members.

What does system IndexOutOfRangeException index was outside the bounds of the array?

The IndexOutOfRangeException is an exception that will be thrown while accessing an element of a collection with an index that is outside of its range. It occurs when an invalid index is used to access a member of a collection.


2 Answers

The HashSet<T> is not thread-safe. Especially when adding items in a multi-threaded scenario and the internal capacity has to be increased, things can go out of sync.

like image 65
herzmeister Avatar answered Nov 05 '22 15:11

herzmeister


The instance methods on HashSet<T> are not thread-safe. In particular, when you attempt to add an element that would cause the set to exceed the bounds of the existing array in more than one thread at a time, the instance variables used to keep track of the size of the set and the last index in the set can be updated in both threads. In particular, if the last index value is updated by the second thread (with a larger value) before the first thread is finished copying the destination array, it could attempt to access an element of the local array that does not exist because the local array was allocated to only hold half as many elements as that allocated by the second thread.

like image 20
tvanfosson Avatar answered Nov 05 '22 15:11

tvanfosson