在C#列表大小限制 [英] List size limitation in C#

查看:270
本文介绍了在C#列表大小限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能可能看起来是一个讨厌的事情要问
但为什么我们有一个列表中的对象数量如此之短的限制。

This could possibly appear to be a nasty thing to ask but why do we have so short limit of number of objects in a list.

我写了下面的code在C#中测试列表大小

i wrote following code to test list size in C#

    List<int> test = new List<int>();            
    long test1 = 0;
    try
    {
        while (true)
        {
            test.Add(1);
            test1++;
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(test1 + "   |   " + ex.Message);
    }

和列表的大小只能是134217728

and the size of list could only be 134217728

是不是不公平?:(是什么另一种方法,如果我想添加的对象甚至超越整数限制(我的意思是对象的数目> 2 ^ 32)???

isn't that unfair :( ??? what is alternate way if i want to add objects even beyond 'integer' limits (i mean number of objects > 2^32) ???

推荐答案

A 列表&LT; INT&GT; 被支持的 INT [] 。您将尽快更大的支持数组不能分配失败 - 并且牢记:

A List<int> is backed by an int[]. You will fail as soon as a larger backing array cannot be allocated - and bear in mind that:


  • 有一个2GB的每个对象限制在CLR即使是在64位(编辑:作为.NET 4.5,这是可以避免的 - 见<一href=\"http://msdn.microsoft.com/en-us/library/hh285054%28v=vs.110%29.aspx\"><$c$c><gcAllowVeryLargeObjects>)

  • 列表将尝试分配一个支持数组比它立即需要较大的,为了以后适应添加请求重新分配没有

  • 在重新分配,必须有两个老的的新的数组。
  • 足够的总内存
  • There's a 2GB per-object limit in the CLR even in 64 bits ( as of .NET 4.5, this can be avoided - see <gcAllowVeryLargeObjects>)
  • The list will try to allocate a backing array which is larger than what it immediately requires, in order to accommodate later Add requests without reallocation.
  • During the reallocation, there has to be enough total memory for both the old and the new arrays.

设置 容量 来这将对支持数组接近理论极限的值可能会比你自然增长率较高的临界点,但限制肯定会来的。

Setting the Capacity to a value which will put the backing array near the theoretical limit may get you a higher cutoff point than the natural growth, but that limit will certainly come.

我的期望的大约2极限 29 元素(536,870,912) - 我微微一惊你还没有成功地超越134217728。多少内存你真的有吗?您使用的是和什么什么样的架构.NET版本的? (这可能是每个对象限制为1GB的32位CLR,我不记得是肯定的。)

I would expect a limit of around 229 elements (536,870,912) - I'm slightly surprised you haven't managed to get beyond 134,217,728. How much memory do you actually have? What version of .NET are you using, and on what architecture? (It's possible that the per-object limit is 1GB for a 32-bit CLR, I can't remember for sure.)

请注意,即使每个对象的限制,尽快不是一个问题,因为你在上面得到2 你有31 元素的问题直接寻址的那些元素与列表&LT; T&GT; ,因为索引需要一个 INT

Note that even if the per-object limit wasn't a problem, as soon as you got above 231 elements you'd have problems addressing those elements directly with List<T>, as the indexer takes an int value.

基本上,如果你想比 int.MaxValue 更多元素的集合,你需要编写自己的,可能使用多个后备阵列。你可能想明确禁止采伐和任意插入:)

Basically, if you want a collection with more than int.MaxValue elements, you'll need to write your own, probably using multiple backing arrays. You might want to explicitly prohibit removals and arbitrary insertions :)

这篇关于在C#列表大小限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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