What's the max items in a List?

后端 未结 6 1376
闹比i
闹比i 2020-11-29 08:47

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 me

相关标签:
6条回答
  • 2020-11-29 09:34

    The List limit is 2.1 Billion objects or the size of your memory which ever is hit first.

    0 讨论(0)
  • 2020-11-29 09:35

    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:

    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;
        }
    }
    
    0 讨论(0)
  • 2020-11-29 09:37

    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.

    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.

    0 讨论(0)
  • 2020-11-29 09:41

    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.

    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.


    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 structs.

    0 讨论(0)
  • 2020-11-29 09:50

    The List will dynamically grow itself to accomodate as many items as you want to include - until you exceed available memory!

    From MSDN documentation:

    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.

    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.

    0 讨论(0)
  • 2020-11-29 09:52

    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

    0 讨论(0)
提交回复
热议问题