-2

How can we predict or calculate the max number of object a list can contains List<Model> in c#? A model has many attributes like string int double float compostiedatatype.

if we have a List<int> it is simple straight forward it's max size is 2^32, what if the object is composite data type? should we calculate the byte of each property of model and it sum it?

object
{
int byte
double  byte
string  byte 
}

size of object is = calculate(add each byte of property) ??

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

EDIT What i can say from the below code?

While(true)
{
  try
   {
      listModel.add({add model});
   }
   catch(Exception overflow)
   {
      listModel.Count();
   }
}

If the count is 32 so i can predict the list can contain only 32 objects?

Why i am wondering about memory

Should i care about memory, assume i have a list of objects in memory every time the user inserted we insert the data in to the list i am wondering is there a point where my application will collapse? Should i reconsider my approach? In memory data has fast retrieval time like dictionary retrieval time is O(1).??

John Doe
  • 141
  • 1
  • 2
  • 10
  • 1
    The accepted answer on the question you've linked already mentions that for reference types only a reference is stored in the list, of either 4 or 8 bytes each. – C.Evenhuis Dec 22 '17 at 13:24
  • it means we have to calculate the size of each property for a model as i mentioned in the question? – John Doe Dec 22 '17 at 13:26
  • @JohnDoe it depends on what you're trying to calculate. If you want to predict the memory usage of the entire program, you'd probably have to do what you're suggesting. However the OS has a lot of tricks up it's sleeve to alter it's usage. If you're specifically trying to predict the memory usage of the List object, then you can sum the memory usage of the pointers saved in the List. The List doesn't store the actual objects, it only contains references to the objects you've created. – Glubus Dec 22 '17 at 13:40
  • @Glubus i have edit my question to be more specific. – John Doe Dec 22 '17 at 13:54
  • @JohnDoe I see, but my answer and CEvenhuis' answer is still revelant. Can I assume you're not a fluent english speaker or that you might not fully understand our answers? Otherwise I don't think I understand what you're trying to ask. – Glubus Dec 22 '17 at 13:58
  • @Glubus i got your point now i have update my question why should i wonder about memory. – John Doe Dec 22 '17 at 14:03
  • @JohnDoe *why should I wonder about memory* - because you would be able to have an infinite amount of items in your list if you were not bound by the number of addresses in your working memory (i.e. RAM). – Glubus Dec 22 '17 at 14:06
  • Please read edit part of my question i have real problem. Any assistance would be highly appreciate – John Doe Dec 22 '17 at 14:08
  • Perhaps, it's always better to persist your data instead of solely using memory.... – zc246 Dec 22 '17 at 14:15

1 Answers1

0

After discussing your question for a while in the comments, I guess I'll try to answer it as best as I can. It's rather difficult to understand what you're asking, as it touches some elementary stuff that I doubt is something you don't know as well as the fact that your grammar is really bad.

If the count is 32 so i can predict the list can contain only 32 objects? What do you mean if the count is 32? If you do this: new List<object>(32);, you only instantiate the array behind the List to a size of 32. A list is an array behind the scenes, with some added functionality like .Add(). If you add an object for the 33th time, it will create a new array with a larger size, and copy all objects to the new array, allowing you to keep adding new objects.

If you keep adding and adding, obviously your program will run out of memory. In case of a webapplication, most people use some kind of persistance like a database (or even the filesystem). If you're worried about your program running out of memory, you should not use a List and just use a database, which just uses all the space on your hard disk. is there a point where my application will collapse - like I said your program will give a OutOfMemoryException if your List becomes too large.

In memory data has fast retrieval time like dictionary retrieval time is O(1) - memory usage has nothing to do with time complexity (i.e. O(1)). A List is stored in memory, which allows for relatively (relative to the harddisk) fast IO read actions. O(1) time says something about algorithmic complexity. For instance, accessing an array in C# is an O(1) complex action.

Glubus
  • 2,819
  • 1
  • 12
  • 26