List<T> 中的最大项目是多少? [英] What&#39;s the max items in a List&lt;T&gt;?

查看:22
本文介绍了List<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)

当整数列表中的 Count = 134217728 时,出现内存不足异常.得到 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

推荐答案

List 将限制为数组的最大值,即 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 使用加倍算法;如果您通过 Add 滴灌项目(没有先分配所有空间),它将尝试加倍到 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 中,如果 开关已启用.限制是 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.

这篇关于List<T> 中的最大项目是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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