what is the max limit of data into list<string> in c#?
Asked Answered
P

5

63

How many values I can add to List?

For example:

List<string> Item = runtime data

The data is not fixed in size. It may be 10 000 or more than 1 000 000. I have Googled but have not found an exact answer.

Ploce answered 11/10, 2010 at 13:59 Comment(0)
A
93

The maximum number of elements that can be stored in the current implementation of List<T> is, theoretically, Int32.MaxValue - just over 2 billion.

In the current Microsoft implementation of the CLR there's a 2GB maximum object size limit. (It's possible that other implementations, for example Mono, don't have this restriction.)

Your particular list contains strings, which are reference types. The size of a reference will be 4 or 8 bytes, depending on whether you're running on a 32-bit or 64-bit system. This means that the practical limit to the number of strings you could store will be roughly 536 million on 32-bit or 268 million on 64-bit.

In practice, you'll most likely run out of allocable memory before you reach those limits, especially if you're running on a 32-bit system.

Amphiboly answered 11/10, 2010 at 14:3 Comment(1)
In addition to the fixed upper limit, creating and releasing large arrays repeatedly can lead to inconsistent allocation where even more reasonable allocations (on the order of a few 100 MB) can cause OutOfMemoryException, even though the memory is actually available. If you have to deal with very large collections, this will impact your design.Highclass
G
13

2147483647 because all functions off List are using int.

Source from mscorlib:

private T[] _items;
private int _size;

public T this[int index]
{
  get
    {
      //...
    }
}
Gonidium answered 11/10, 2010 at 14:5 Comment(0)
T
4

list.Count() property is int32, so it must be the maxium limit of int32 but how your list performs over this limit is a nice observation.

if you do some list manupulation operations, it would be linier in theory.

i would say if your are having very large number of items thnink about the Parallel Collections in .net 4.0 this would make your list operations more responsive.

Triserial answered 11/10, 2010 at 14:25 Comment(0)
P
1

As per the implementation of List

private void EnsureCapacity(int min) {
    if (_items.Length < min) {
        int newCapacity = _items.Length == 0? _defaultCapacity : _items.Length * 2;
        // Allow the list to grow to maximum possible capacity (~2G elements) before encountering overflow.
        // Note that this check works even when _items.Length overflowed thanks to the (uint) cast
        if ((uint)newCapacity > Array.MaxArrayLength) newCapacity = Array.MaxArrayLength;
            if (newCapacity < min) newCapacity = min;
                Capacity = newCapacity;
        }
    }

Now upon navigating to this Array.MaxArrayLength:

internal const int MaxArrayLength = 2146435071;
Polysynthetic answered 11/10, 2021 at 16:37 Comment(0)
M
-2

You can get around this limit easily with a List<char[]>.

Mitosis answered 9/11, 2021 at 2:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.