我用List< string>击中了OutOfMemoryException -这是极限吗?还是我错过了什么? [英] I hit an OutOfMemoryException with List<string> - is this the limit or am I missing something?

查看:64
本文介绍了我用List< string>击中了OutOfMemoryException -这是极限吗?还是我错过了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给我重写的机会,但无论如何,我会照原样编写代码:

Given the opportunity to rewrite, I would, but anyway, the code as it stands:

List<string> foobar;

然后我们在foobar中添加一串字符串.

Then we add a bunch of strings to foobar.

在count = 16777216处,我们达到了内存不足的限制.

At count=16777216, we hit an out of memory limit.

我的理解是,每个字符串的大小都会不同.实际上,查看数据(不是我的数据)时,大多数是2个或3个字符.

My understanding is that each string would be a different size. Indeed looking at the data (not my data), most are 2 or 3 characters.

c#中列表数据的最大限制是多少?表示最大限制是:

当前可以存储的最大元素数 从理论上讲,List的实现是Int32.MaxValue-只是 超过20亿.

The maximum number of elements that can be stored in the current implementation of List is, theoretically, Int32.MaxValue - just over 2 billion.

但是:

在当前的CLR的Microsoft实现中,有2GB 最大对象大小限制. (其他实现有可能, 例如Mono,没有此限制.)

In the current Microsoft implementation of the CLR there's a 2GB maximum object size limit. (It's possible that other implementations, for example Mono, don't have this restriction.)

在我的示例中,我有1600万个结果*几个字节?任务管理器显示了正在使用的演出,但是我有8演出的RAM.

In my example, I have, what, 16 million results * a few bytes? Task manager shows about a gig being used, but I have 8 gigs of RAM.

16777216(2 ^ 24)似乎是一个相当具体的值-可疑地像是一个限制,但我找不到任何文档来a)支持这一点或b)找到解决方法?

16777216 (2^24) seems like a fairly specific value - suspiciously like a limit, but I can't find any documentation anywhere to a) back this up or b) find a way around it?

任何帮助将不胜感激.

一些代码:

List<string> returnList = new List<string>();
SqlDataReader dr; //  executes a read on a database, have removed that part as that bit works fine

  if (dr.HasRows)
  {
      while (dr.Read())
      {
          returnList.Add(dr.GetString(0).Trim());
      }
  }

这是简化形式,我现在对OOM异常有了一些try/catch,但这是使我感到悲伤的实际代码.

That's the simplified form, I now have some try/catch for the OOM Exception, but this is the actual code that's giving me grief.

推荐答案

如果要在64位环境中使用非常大的列表,则需要在应用程序配置中启用大对象.

If you're trying to use very large lists in 64 bit environments you need to enable large objects in the application configuration.

http://msdn.microsoft.com/en-us/library/hh285054.aspx

OOM可能是由于Lists/ArrayLists分配内存的方式所致,我相信每次到达其边界时,它们都会尝试将其大小加倍.列表不能从2 ^ 24翻倍.从理论上讲,您可以通过预先指定大小来最大化列表大小. (即2GB)

The OOM is likely due to the way Lists/ArrayLists allocate memory, which I believe is each time their boundary is reached, they attempt to double in size. The list cannot double from 2^24. You could theoretically maximize your list size by pre-specifying a size. (I.e. 2GB)

这篇关于我用List&lt; string&gt;击中了OutOfMemoryException -这是极限吗?还是我错过了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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