开发者

What's the max items in a List<T>?

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

How do I in开发者_如何学JAVAcrease 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


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.


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


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


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;
    }
}


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜