添加到Hashset< T>中时出现IndexOutOfRangeException [英] IndexOutOfRangeException when adding to Hashset<T>

查看:78
本文介绍了添加到Hashset< T>中时出现IndexOutOfRangeException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的应用程序,它向HashSet <string>中添加了大约700万个短字符串.有时在调用Hashset.Add()时遇到异常:System.Collections.Generic.HashSet`1.IncreaseCapacity():索引超出数组的范围.

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.

这是一个间歇性的问题,似乎与内存有关,但这是在具有16 GB的win2k8 R2服务器上,没有太多其他事情了,大部分物理内存都可用.有什么想法吗?

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?

推荐答案

HashSet<T>不是线程安全的.尤其是在多线程方案中添加项目并且必须增加内部容量时,事情可能会不同步.

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&lt; T&gt;中时出现IndexOutOfRangeException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆