What's the Max Items in a List<T>

What's the max items in a ListT?

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.

what is the max limit of data into liststring in c#?

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.

Finding max value from within ListT

The best method would be to create an interface with the specific functionality and have all of the classes implement that interface:

public interface ICreated
{
public DateTime CreateDate {get;}
}

Then you can ensure that all of the items accepted implement that interface:

private static DateTime GetLastDate<T>(IEnumerable<T> input) where T : ICreated
{
return input.Max(d=>d.CreateDate);
}

If that's really not an option (possibly because you can't modify the class to have it implement the interface or the collection to wrap the underlying type) you could use dynamic. I would highly discourage that you do this as it's really not good design, it will be much slower, and it's rather susceptible to breaking, but it could work:

private static DateTime GetLastDate(IEnumerable<dynamic> input)
{
return input.Max(d=>d.CreateDate);
}

List size limitation in C#

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:

  • There's a 2GB per-object limit in the CLR even in 64 bits (EDIT: as of .NET 4.5, this can be avoided for the 64-bit CLR - 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.

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

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.

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 :)

ListT -- Proper Collection to Use for Large Amount of Items?

Where were you expecting to store all this data? List<T> will always store its values in memory... but even if you write something to store the results to disk, you're still not going to be able to hold 1.4e122 items. Have you really taken in just how big that number is? Even at a single bit per item, that's way more than the capacity of the universe, if the whole of the universe was one big hard disk.

The largest unit of data I've ever heard of being talked about in a meaningful way is an exabyte, which is 1018 bytes. For most people, a petabyte (1015 bytes) is a pretty huge amount of data. What you're considering makes those quantities seem microscopically small.

What are you trying to do with the data afterwards? And when would you expect such an algorithm to ever actually finish?

How to get the max value of a function in a list of objects (C#) ? -Beginner-

You need to use maximumArea to store the max of the list, not the max of the current pair.

Trapezoid maximumArea = new Square(0);  //Smallest possible trapezoid

foreach (Trapezoid t in Shape)
{
if (t.Area() > maximumArea.Area()) maximumArea = t;
}
Console.WriteLine(maximumArea);

You can also shorten this considerably using LINQ:

var maximumArea = shape.OrderByDescending( x => x.Area() ).First();

And if there's a chance that some trapezoids will tie, maybe you should get a list:

var max = shape.Max( x => x.Area() );
var maximumAreas = shape.Where( x => x.Area() == max).ToList();

How much data can a List can hold at the maximum?

It depends on the List implementation. Since you index arrays with ints, an ArrayList can't hold more than Integer.MAX_VALUE elements. A LinkedList isn't limited in the same way, though, and can contain any amount of elements.

Get Max value from ListmyType

Okay, so if you don't have LINQ, you could hard-code it:

public int FindMaxAge(List<MyType> list)
{
if (list.Count == 0)
{
throw new InvalidOperationException("Empty list");
}
int maxAge = int.MinValue;
foreach (MyType type in list)
{
if (type.Age > maxAge)
{
maxAge = type.Age;
}
}
return maxAge;
}

Or you could write a more general version, reusable across lots of list types:

public int FindMaxValue<T>(List<T> list, Converter<T, int> projection)
{
if (list.Count == 0)
{
throw new InvalidOperationException("Empty list");
}
int maxValue = int.MinValue;
foreach (T item in list)
{
int value = projection(item);
if (value > maxValue)
{
maxValue = value;
}
}
return maxValue;
}

You can use this with:

// C# 2
int maxAge = FindMaxValue(list, delegate(MyType x) { return x.Age; });

// C# 3
int maxAge = FindMaxValue(list, x => x.Age);

Or you could use LINQBridge :)

In each case, you can return the if block with a simple call to Math.Max if you want. For example:

foreach (T item in list)
{
maxValue = Math.Max(maxValue, projection(item));
}

Get max value from a list with lists?

Loop through your outer list and select the last element of each sublist:

def max_value(inputlist):
return max([sublist[-1] for sublist in inputlist])

print max_value(resultlist)
# 9.1931

It's also best if you keep all function related variables in-scope (pass the list as an argument and don't confuse the namespace by reusing variable names).



Related Topics



Leave a reply



Submit