List Memory Allocation

Source: Internet
Author: User

When the default constructor list <int> value = new list <int> () is used to instantiate a list <t> object ,. net Framework only applies a piece of memory in the memory to store the list <t> object itself (excluding the items element in the list ).

When the first item element is added to a list <t> object, the list <t> Object applies for memory space that can store four item elements, then, the item element is stored in the requested space.

The list <t> object has a capacity attribute to indicate the capacity of the current list <t> Object (that is, the number of items currently used to store the item's memory space ), you can manually set the capacity of the List <t> (a change in capacity will result in a new memory request ).

When the number of item elements of a list <t> Object exceeds the number of capacity, the list <t> object will re-apply for a memory space that is twice the size of the original capacity, then, all the items and the elements to be added are copied to the new memory space.

The following test code verifies the memory allocation principle of list <t>:

List <int> value = new list <int> (4); // count: 0 capacity: 4 for (INT I = 1; I <= 5; I ++) {value. add (I);} // count: 5 Capacity: 8 value. trimexcess (); // count: 5 Capacity: 5 // remove an itemvalue. removeat (4); // count: 4 Capacity: 5value. trimexcess (); // count: 4 Capacity: 5 ---------------Why isn't capacity 4? Expert advice

// Remove another itemvalue. removeat (1); // count: 3 Capacity: 5value. trimexcess (); // count: 3 Capacity: 3value. clear (); // count: 0 capacity: 3value. trimexcess (); // count: 0 capacity: 0

 

Knowing the principles of memory allocation, we need to follow these principles to adopt the optimal method to ensure that the limited memory space can be used properly. To sum up, there are mainly the following points:

  1. When instantiating a list <t> object, if you can predict the approximate number of its item elements, when instantiating a list <t> object, set its capacity value to the minimum value close to the number of item elements. In this way, you can avoid applying for memory and element replication when adding elements to the list <t>.
  2. When the number of item elements is much smaller than capacity due to the constant call of the remove method, the memory will be wasted. You can call the trimexcess method to Release excess memory.

 

[Appendix] Some attributes and members of the List class in msdn are as follows:

// Abstract: // obtain or set the total number of elements that the internal data structure can accommodate without resizing. //// Returned result: // The number of elements that can be accommodated by system. Collections. Generic. List <t> before the size is adjusted. //// Exception: // system. argumentoutofrangeexception: // system. collections. generic. list <t>. capacity is set to smaller than system. collections. generic. list <t>. the value of Count. /// System. outofmemoryexception: // The system does not have enough available memory. Public int capacity {Get; set;} // Abstract: // gets the number of elements actually contained in system. Collections. Generic. List <t>. //// Returned result: // number of elements actually contained in system. Collections. Generic. List <t>. Public int count {Get;} // Abstract: // Add the object to the end of system. Collections. Generic. List <t>. //// Parameter: // item: // the object to be added to the end of system. Collections. Generic. List <t>. For the reference type, this value can be null. Public void add (T item); // Abstract: // remove the first matching item of a specific object from system. Collections. Generic. List <t>. //// Parameter: // item: // the object to be removed from system. Collections. Generic. List <t>. For the reference type, this value can be null. //// Return result: // true if the item is successfully removed; otherwise, false. If no // item is found in system. Collections. Generic. List <t>, false is returned for this method. [Targetedpatchingoptout ("performance critical to inline upload SS ngen image boundaries")] public bool remove (T item); // Summary: // set the capacity to system. collections. generic. the actual number of elements in list <t> (if the number is smaller than a threshold value ). Public void trimexcess ();

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.