C# List<T> 中的最大项目是多少?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2009217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the max items in a List<T>?
提问by Tony The Lion
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)
我如何增加这个尺寸?或者是否有一个包含无限项目的集合?(尽可能多地适合内存,即)
EDIT:
编辑:
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
当整数列表中的 Count = 134217728 时,出现内存不足异常。得到 3Gb 的 RAM,其中 2.2 正在使用中。声音正常
采纳答案by Marc Gravell
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.
List<T>
将被限制为数组的最大值,即 2GB(即使在 x64 中)。如果这还不够,则说明您使用了错误的数据存储类型。不过,您可以通过以正确的大小启动它来节省大量开销 - 通过将 an 传递int
给构造函数。
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 deathwhile adding items.
重新编辑 - 134217728 x Int32,即 512MB。请记住,List<T>
使用加倍算法;如果您通过Add
(不首先分配所有空间)滴灌项目,它将尝试加倍到 1GB(在您已经拥有的 512MB 之上,您的应用程序的其余部分,当然还有 CLR 运行时和图书馆)。我假设你在x86,所以你已经有一个2GB的限制每个进程,而且很可能你已经支离破碎你的“大对象堆”到死亡,同时增加项目。
Personally, yes, it sounds about right to start getting an out-of-memory at this point.
就个人而言,是的,此时开始出现内存不足听起来是正确的。
Edit: 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 struct
s.
编辑:在 .NET 4.5 中,如果<gcAllowVeryLargeObjects>
启用了开关,则允许大于 2GB 的阵列。限制为 2^31 个项目。这对于引用数组(x64 中每个 8 字节)或大struct
s数组可能很有用。
回答by Paul Creasey
It's limited only by memory.
它仅受内存限制。
edit: or not, 2Gb's is the limit! This is quite interesting, BigArray, getting around the 2GB array size limit
编辑:与否,2Gb 是极限!这很有趣,BigArray,绕过 2GB 阵列大小限制
回答by Sapph
The List will dynamically grow itself to accomodate as many items as you want to include - until you exceed available memory!
列表将动态增长以容纳您想要包含的任意数量的项目 - 直到超出可用内存!
From MSDN documentation:
来自 MSDN 文档:
If Count already equals Capacity, the capacity of the List is increased by automatically reallocating the internal array, and the existing elements are copied to the new array before the new element is added.
如果Count已经等于Capacity,则通过自动重新分配内部数组来增加List的容量,并且在添加新元素之前将现有元素复制到新数组中。
If Count is less than Capacity, this method is an O(1) operation. If the capacity needs to be increased to accommodate the new element, this method becomes an O(n) operation, where n is Count.
如果 Count 小于 Capacity,则此方法为 O(1) 操作。如果需要增加容量以容纳新元素,则此方法变为 O(n) 操作,其中 n 是 Count。
回答by Blounty
The List limit is 2.1 Billion objects or the size of your memory which ever is hit first.
List 限制为 21 亿个对象或您的内存大小,以先命中者为准。
回答by Paolo
The interface defines Count and IndexOf etc as type int so I would assume that int.MaxValue or 2,147,483,647 is the most items you could stick in a list.
该接口将 Count 和 IndexOf 等定义为 int 类型,因此我认为 int.MaxValue 或 2,147,483,647 是您可以保留在列表中的最多项目。
Really got to question why on earth you would need that many, there is likely to be a more sensible approach to managing the data.
真的要质疑为什么你需要这么多,可能有更明智的方法来管理数据。
回答by Achilles
On a x64 Machine, using .Net Framework 4 (Not the Client Profile), compiling for Any CPU in Release mode, I could chew up all the available memory. My process is now 5.3GB and I've consumed all available memory (8GB) on my PC. It's actually a Server 2008 R2 x64.
I used a custom Collection class based on CollectionBase
to store 61,910,847 instances of the following class:
在 x64 机器上,使用 .Net Framework 4(不是客户端配置文件),在发布模式下为任何 CPU 编译,我可以咀嚼所有可用内存。我的进程现在是 5.3GB,我已经消耗了我 PC 上的所有可用内存 (8GB)。它实际上是一个 Server 2008 R2 x64。
我使用基于的自定义 Collection 类CollectionBase
来存储以下类的 61,910,847 个实例:
public class AbbreviatedForDrawRecord {
public int DrawId { get; set; }
public long Member_Id { get; set; }
public string FactorySerial { get; set; }
public AbbreviatedForDrawRecord() {
}
public AbbreviatedForDrawRecord(int drawId, long memberId, string factorySerial) {
this.DrawId = drawId;
this.Member_Id = memberId;
this.FactorySerial = factorySerial;
}
}