添加到 Hashset时出现 IndexOutOfRangeException;
我有一个简单的应用程序,它将大约 700 万个短字符串添加到 HashSet
中。有时,我在调用 Hashset.Add() 时会遇到异常:System.Collections.Generic.HashSet`1.IncreaseCapacity():索引超出了数组的范围。
这是一个间歇性问题,似乎与内存有关,但这是在 16 GB 的 win2k8 R2 服务器上,没有什么其他情况,大部分物理内存都是可用的。有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
HashSet
不是线程安全的。特别是在多线程场景中添加项目并且必须增加内部容量时,事情可能会不同步。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.HashSet
上的实例方法不是线程安全的。特别是,当您尝试添加一个元素,该元素会导致集合一次在多个线程中超出现有数组的边界时,用于跟踪集合大小和最后一个索引的实例变量该集合可以在两个线程中更新。特别是,如果在第一个线程完成复制目标数组之前第二个线程更新了最后一个索引值(具有更大的值),则它可能会尝试访问本地数组中不存在的元素,因为本地数组分配的元素数量仅为第二个线程分配的一半。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.