4

Given a list:

List<myType> myList = new List<myType>();

with the collection contents modified at runtime....

To clear the list, I have seem examples which suggest using

myList = new List<myType>();

rather than

myList.Clear();

I can think of a number of reasons why I would prefer one over the other but are there any other good benchmarks or guidelines as to when one method is preferable to the other?

braX
  • 11,506
  • 5
  • 20
  • 33
Matthew
  • 10,244
  • 5
  • 49
  • 104

3 Answers3

3

If a list is large (80 kilobytes or more), then it's going to be stored on the large object heap. The official guidance for the LOH is to re-use objects there as much as possible in order to reduce heap fragmentation. The LOH isn't compacted like the regular heap is.

For smaller lists, I've found that it's often faster overall to create a new one than it is to call Clear. This isn't always true, so you're probably best off testing it both ways in your applications.

If you call Clear, it just sets all of the items in the list to their default values and sets Count to 0. It does not change the list's capacity. So calling Clear will not change the amount of memory allocated to the collection. If you want to clear the list and reduce its size, call Clear and then TrimExcess.

One problem you'll run into if you're not careful is aliasing. If you have multiple objects that refer to the same list, creating a new list doesn't remove those other references. So then you end up with two lists. Just something to think about.

All told, I don't think there's a particular "best practice" for this. I've found that sometimes it's good to use Clear and sometimes it's best to allocate a new list.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
  • +1 This is more along the lines of what I'm looking for. Thanks. Do you have a link about the LOH? – Matthew Aug 22 '11 at 18:01
  • @Matthew: See the link I just added. There's a good CLR Inside Out article about it. – Jim Mischel Aug 22 '11 at 18:12
  • I know this is an old question but it just got linked as a duplicate and reading the answer made me wonder something... When you talk about the size of the list how is it calculated? Presumably the size of the list is not the size of the contents of the list since they will all be references (if it is a list of reference types) so it will just be the size of all the references (and offhand I forget how big that will be)...? – Chris Jul 23 '16 at 23:13
  • @Chris: The size of a list will be its `Capacity` times the size of a reference (assuming it's a list of reference type), plus a small amount (less than 100 bytes) for overhead. A reference is 4 bytes in the 32 bit runtime, and 8 bytes in the 64 bit runtime. So a list with a capacity of 20,000 will be approximately 80 KB or 160 KB, depending on the runtime. – Jim Mischel Jul 24 '16 at 08:08
2

When you are binding to the list object and have references to it from other areas of code - then use the clear method. If you have no references and are not bound then creating a new object would be appropriate.

tsells
  • 2,751
  • 1
  • 18
  • 20
  • If I want to reference an empty list, why is clearing it more appropriate than just assigning an empty list to the reference? – Matthew Aug 22 '11 at 17:55
  • It really depends on what you are doing with it. If you are just creating an empty list to pass as a parameter to a method to have a reference to it when you return - that is fine. I normally have the List be the return item or an out parameter from the method however. – tsells Aug 24 '11 at 00:42
0

Be careful using Clear. It modifies the actual list. If you have another reference to that same list somewhere, perhaps under a different name, you may have unexpected results.

For example:

List<int> one = new() { 1, 2, 3 };
List<int> two = one;
one.Clear(); // two is now also empty

List<int> three = new() { 1, 2, 3 };
List<int> four = three;
three = new(); // four still has values in it

Therefore, in order to avoid unintended consequences, I prefer to make a new List as the usual practice. Clear can be used in certain cases, just make sure you really want to clear everything that it clears.

DCShannon
  • 2,470
  • 4
  • 19
  • 32