OutOfMemoryException异常增加时,更多的项目非常大的HashSet<的Int32>非常大、异常、项目、更多

2023-09-02 21:47:30 作者:跟自己说抱歉

类型的异常的System.OutOfMemoryException 被抛出,而试图加入一个 HashSet的<的Int32>

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

我们需要维护整数高性能独特的收藏的sizeof 的Int32 .MaxValue即 2147483647 的HashSet 的Int32 只能存储 23997907 在它的项目。寻找一个建议来解决这个问题。

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.

推荐答案

HashSet的(Of T)中物体的能力要素的对象可以容纳的数量。作为元素被添加到它的对象的能力自动增加。

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.

如果您使用的是64位系统,可以提高的Hashset的最多高达2十亿能力   通过的 gcAllowVeryLargeObjects 已启用的属性设置为true在运行时环境中的元素。

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>

勾选此 MSDN 的链接,设置配置。

Check this MSDN link for setting the configuration.

更新:

以上配置的 gcAllowVeryLargeObjects 支持框架上的4.5只。

Above config gcAllowVeryLargeObjects supports on .Net framework 4.5 only.

 
精彩推荐