I was doing some performance optimization inside my app and there were a lot of places where I’m assigning a collection to the itemssource property of a listbox. When I assign the collection to the listbox is the original collection disposed from memory. If not, by setting it to null will it mark the collection for garbage collection?
For example:
void myWebServiceCompleted(object sender, List<Item> itemList)
{
ItemListBox.ItemsSource = itemList;
//Would setting itemList = null clear up resources?
}
I was just concerned because this type of assignment is made in multiple places in the app and if the above is true that means twice the memory.
No.
the ListBox holds a reference to your original list, so it is not garbage collected.
If for whatever reason you wanted the original list to get collected, you could manually add ListItems to the ListBox.
In this case the original list could get collected as long as there were no other references.
I guess this would be useful if each of the items in your collection was resource-heavy, and the listbox only needed to know a couple of fields (name and id for example)