Hi @Jane Jie Chen , Welcome to Microsoft Q&A,
Collection.Clear() is an important operation, but it is not the only aspect to consider.
Collection.Clear() removes all referenced objects from the collection, but it only breaks the references between the collection and these objects. If there are no other references to these objects, they will be reclaimed by the garbage collector. If the objects in the collection are referenced by other places, they will not be reclaimed even if Clear() is called.
Try to avoid static collections. Because the life cycle of static collections is the same as that of the application, if they are not cleaned up manually, memory leaks will occur.
If objects can be reused, consider using an object pool instead of constantly creating new objects, thereby reducing the burden of memory allocation and garbage collection.
If the objects in the collection implement IDisposable, the Dispose method of each object should be called when cleaning the collection.
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.