列表中的最大项目是什么< T>? [英] What's the max items in a List<T>?

查看:136
本文介绍了列表中的最大项目是什么< T>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都知道列表中的最大项目数是多少?

Anybody know what the max number of items in a List is?

如何增加该尺寸?还是有一个收集无限项目的集合? (尽可能多的适合内存,也就是)

How do I increase that size? Or is there a collection that takes infinite items? (as much as would fit in memory, that is)

编辑:

exception当Count = 134217728在一个ints列表中。得到3Gb的RAM,其中2.2正在使用。声音正常

I get an out of memory exception when Count = 134217728 in a list of ints. got 3Gb of RAM of which 2.2 are in use. Sound normal

推荐答案

列表< T> 的数组,这是2GB(甚至在x64)。如果这还不够,您使用的数据存储类型不正确。您可以通过将 int 传递给构造函数来节省大量的开销。

List<T> will be limited to the max of an array, which is 2GB (even in x64). If that isn't enough, you're using the wrong type of data storage. You can save a lot of overhead by starting it the right size, though - by passing an int to the constructor.

重新编辑 - 用134217728 x Int32,也就是512MB。记住, List< T> 使用加倍算法;如果你是通过添加(没有分配所有的空间首先)滴灌项目,它将尝试双倍到1GB(在你已经持有的512MB之上,您的应用程序的其余部分,当然是CLR运行时和库)。我假设你在x86上,所以你已经有每个进程2GB的限制 ,可能是你的大对象堆碎片到死亡而添加项目。

Re your edit - with 134217728 x Int32, that is 512MB. Remember that List<T> uses a doubling algorithm; if you are drip-feeding items via Add (without allocating all the space first) it is going to try to double to 1GB (on top of the 512MB you're already holding, the rest of your app, and of course the CLR runtime and libraries). I'm assuming you're on x86, so you already have a 2GB limit per process, and it is likely that you have fragmented your "large object heap" to death while adding items.

个人,是的,这听起来有权开始获得内存不足。

Personally, yes, it sounds about right to start getting an out-of-memory at this point.

编辑:在.NET 4.5中,如果 < gcAllowVeryLargeObjects> 开关。那么限制是2 ^ 31项。这可能对引用数组(每个在x64中为8个字节)或大型 struct 的数组有用。

in .NET 4.5, arrays larger than 2GB are allowed if the <gcAllowVeryLargeObjects> switch is enabled. The limit then is 2^31 items. This might be useful for arrays of references (8 bytes each in x64), or an array of large structs.

这篇关于列表中的最大项目是什么&lt; T&gt;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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