Memory allocation for List
-
How the memory is allocated in heap for a dynamically growing List. while creating a new object the number of bytes required for that type is allocated in heap. How this is working if that class has a dynamic list???
My small attempt...
-
How the memory is allocated in heap for a dynamically growing List. while creating a new object the number of bytes required for that type is allocated in heap. How this is working if that class has a dynamic list???
My small attempt...
Find memory allocation detail in following links... http://www.codeguru.com/columns/dotnet/article.php/c6593/[^] Read following section in above link Allocation and Garbage Collection Details For generalized information about memory allocation by .NET http://www.dotnet-guide.com/exploring-different-stages-of-memory-management-in-net.html[^] HTH
Jinal Desai - LIVE Experience is mother of sage....
-
How the memory is allocated in heap for a dynamically growing List. while creating a new object the number of bytes required for that type is allocated in heap. How this is working if that class has a dynamic list???
My small attempt...
a List does not store its items, all it stores are references to those items. Basically it stores them in an array. When the array gets full, a new array is allocated, twice the size, and its content (the references!) is copied to the new array. Objects never grow. They have a fixed size in memory. Assuming Car and Wheel are classes, when a Car object has four Wheel objects, it holds 4 references to those Wheel instances, which are separate objects. If Wheel where a struct (hence a value type, not a reference type), the Car would hold memory to store 4 Wheel instances all the time, whether you initialize them or not. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
-
How the memory is allocated in heap for a dynamically growing List. while creating a new object the number of bytes required for that type is allocated in heap. How this is working if that class has a dynamic list???
My small attempt...
-
It is like a linked list(not really.. but for the memory allocation part). It can have any number of items while memory is free. It is like using
malloc
functionpranav95 wrote:
It is like a linked list
Not at all. A linked list would consist of individual items, each holding one or two pointers to its logical neighbors, and would imply: a separate memory block for each item, no overall memory requirement (except maybe for a head node and a tail node), and certainly no memory requirement larger than the item size itself. A .NET collection is based on an array, most additions do not require any memory requirement at all, and the array's capacity grows by doubling it, which may cause it to become located in the "large object heap" and may even cause fragmentation and out-of-memory situations one would not have with a linked list; OTOH being array-based has advantages, e.g. it allows for an O(1) indexing. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).