Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will List<T> Shrink In Size If You Remove Elements

Tags:

When a List<T> gets full, it doubles in size, occupying twice the memory, but would it automatically decrease in size if you removed elements from it?

As much as I understand decreasing the Capacity would not mean relocating all the data in memory, it would just need to drop off the end of the reserved memory, but does it actually do it?

like image 552
Aleksei Maide Avatar asked Sep 13 '16 18:09

Aleksei Maide


People also ask

How to remove an element at particular index in Python?

You can use the pop() method to remove specific elements of a list. pop() method takes the index value as a parameter and removes the element at the specified index. Therefore, a[2] contains 3 and pop() removes and returns the same as output. You can also use negative index values.

How to remove elements from array in Python?

You can use the pop() method to remove an element from the array.


1 Answers

No, List doesn't ever decrease the capacity unless you explicitly lower it yourself by setting that property or using TrimExcess, except when you call Clear and it can remove the buffer entirely.

Of course, that's just the current implementation, and it is an implementation detail, so you cannot rely on it not shrinking the backing array.

like image 68
Servy Avatar answered Jan 03 '23 09:01

Servy