- Home /
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.
MyClass { 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?
Thanks
Answer by Eric5h5 · Jul 14, 2012 at 10:50 PM
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.
Thank you. Do I have to also use destroy on the unity objects the class creates, or set any variables to null in addition to using remove?
Yes, Unity objects are not handled by $$anonymous$$ono, so you need to Destroy them. Setting variables to null doesn't do anything, since they are marked for garbage collection when they go out of scope anyway. Setting to null discouraged in .NET, at least (for the purposes of garbage collection); not sure if $$anonymous$$ono works the same way,
Thanks again for the clarifications. Would anything change if I'm working on an ios project?
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Remove a destroy object from a list, then reload the list 2 Answers
Removing a class object from a list(.remove not working) C# 1 Answer
Memory leaks? 2 Answers