非常大集合在.net导致了内存不足异常 [英] Very large collection in .Net causes out-of-memory exception

查看:518
本文介绍了非常大集合在.net导致了内存不足异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我测试的集合能有多大是.NET。从技术上讲,任何集合对象可以增长到物理存储器的大小。

I am testing how big a collection could be in .Net. Technically, any collection object could grows to the size of the physical memory.

然后,我在一个SEVER,其中有16GB的内存,运行Windows 2003 Server和Visual Studio 2008。我测试了F#和C#code测试了以下code,又看了看任务管理器运行时。我可以看到,经过大约增长的2GB内存,具有内存不足的异常程序崩溃。我没有设置目标平台,在属性页到x64。

Then I tested the following code in a sever, which has 16GB memory, running Windows 2003 server and Visual Studio 2008. I tested both F# and C# code, and looked at the Task Manager while running. I can see that after about growing 2GB memory, the program crashed with out-of-memory exception. I did set the target platform to x64 in the property page.

open System.Collections.Generic

let d = new Dictionary<int, int>()

for i=1 to 1000000000 do
    d.Add(i,i)

我做了同样的测试到 C5 的收藏库。其结果是,在C5中的字典可以使用了整个存储器。在code采用C5:

I did a same test to the C5 collection library. The result is that the dictionary in C5 could use up the whole memory. The code uses C5:

let d = C5.HashDictionary<int, int> ()
for i=1 to 1000000000 do
    d.Add(i,i)

任何人都知道这是为什么?

Anyone knows why?

推荐答案

微软CLR有一个2GB的最大对象大小限制,即使是64位版本。 (我不知道这个限制是否也present在其他实现如单。)

The Microsoft CLR has a 2GB maximum object size limit, even the 64 bit version. (I'm not sure whether this limit is also present in other implementations such as Mono.)

该限制适用于每一个的的对象 - 所有对象不是总规模 - 这意味着它是相对容易使用某种类型的复合式集合要解决

The limitation applies to each single object -- not the total size of all objects -- which means that it's relatively easy to workaround using a composite collection of some sort.

有一个讨论和一些例如code在这里...

There's a discussion and some example code here...

似乎有一个指向这个限制很少的正式文件。这是,毕竟目前的CLR的只是实现的细节。我所知道的唯一一提的是在此页面

There seems to be very little official documentation that refers to this limit. It is, after all, just an implementation detail of the current CLR. The only mention that I'm aware of is on this page:

当您在运行64位管理   在64位Windows应用程序   操作系统,您可以创建一个   不超过2千兆字节对象   (GB)。

When you run a 64-bit managed application on a 64-bit Windows operating system, you can create an object of no more than 2 gigabytes (GB).

这篇关于非常大集合在.net导致了内存不足异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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