how do I remove a class item from a list?

What’s the proper way to remove an item that is a class from a list and free up the memory.

//The class can be composed of unity objects and also large strings which can take large amounts of memory.

var go:GameObject;
var largedata:String;

var classList=new List.();

What’s the best way to remove an item from classList and also free up all memory associated with the class item?


See the MSDN docs for List, namely the Remove commands. If an item in the List is no longer referenced, then the garbage collector will take care of it eventually. The List itself won’t free memory, since List is actually backed by an array, which (unless you manually specify otherwise) starts with enough space allocated for 4 elements in the List, and doubles that to 8 if you add enough items, then 16, 32, etc., but it doesn’t reduce the size of the array if you remove items. If you have a huge List and you remove a lot of items, and want to get back some memory, see the TrimExcess command. Note that classes are by reference anyway, so the List is only storing references to the actual class instances, not the instances themselves.