tags:

views:

268

answers:

5

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

A: 

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

Paul Creasey
then my memory must be running out fast?!?
Tony
Not just limited by memory, there is a hard limit due to the underlying array backing.
Paolo
+20  A: 

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.

Marc Gravell
You mean 2^31 elements, right?
Adam Goode
No, I mean 2GB of memory, as imposed by the CLR. For reference-types, that means (on x64) 8 bytes per reference, so divide it a few more times ;-p
Marc Gravell
An interesting side-effect of the above is that **in theory** you could have more references in an array in x86. In reality, on x86 you're never going to manage to allocate an array that big *and* have space for any useful (and different) objects to put in it... In either case, a list/array of this size is just plain wrong.
Marc Gravell
I've never seen a single .NET process allocate more than about 1.5Gb (at least within a single AppDomain). Most .NET processes seem to run out of memory in their local address space well before they reach the limit of array sizes for storing references.
LBushkin
For more details see this question http://stackoverflow.com/questions/1087982/single-objects-still-limited-to-2-gb-in-size-in-clr-4-0
Brian Rasmussen
BTW, doesn't the Mono CLR allow you to allocate array larger than 2Gb. I thought they support this, and actually implement the Array.LongLength property as a result.
LBushkin
Thanks Brian - the accepted answer has some points that tie in directly to the issue of approaching 1GB. Good reference.
Marc Gravell
Thanks Marc!! :)
Tony
+1  A: 

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.

Sapph
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?
Tony
Looks like you're using half a gigabyte (that many ints * 4 bytes/int32) just for that list. You're probably pushing the boundary there. Do you NEED Count to be that high? You can initialize the list without Count.
Sapph
The count just gets that high, but I can solve this another way. Thanks for all your answers though. It helped me!
Tony
Happy to help. You could also reduce the memory footprint by using a list of bytes or int16, instead of just "int". Would still be quite a bit though, and of course it'd depend on those types being suitable for your needs.
Sapph
A: 

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

Blounty
No that is not correct. No object can be larger than 2 GB so the array holding the actual references or values will limit this significantly.
Brian Rasmussen
+1  A: 

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.

Paolo