Disposing of EntityCollection<T>

Posts   
 
    
NickT
User
Posts: 4
Joined: 26-May-2017
# Posted on: 13-Oct-2017 00:06:15   

When is it necessary to dispose of an EntityCollection? It inherits CollectionCore<T> which implements IDisposable. What resources is EntityCollection using that need to be disposed?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 13-Oct-2017 08:14:57   

CollectionCore.Dispose releases unmanaged and managed resources. IMHO you don't need to worry about that if your EntityCollections go out of scope eventually, which will dispose them automatically. What is your concern about this?

David Elizondo | LLBLGen Support Team
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 13-Oct-2017 09:51:51   

The Dispose() cleans up eventhandlers which are tied to the entities in the collection. So if you keep an entity around which is in a collection, and you get rid of the collection without removing the entity from that collection then the eventhandlers in the collection bound to the events of the entity will keep the collection around (as the entity event references the handler method of the collection). Dispose will unbind the event handlers of the collection bound to the events of every entity in the collection, so if you keep the entities in that collection around, the collection is not kept in memory because of that. It's an edge case, really. Normally you won't run into that situation that much.

Frans Bouma | Lead developer LLBLGen Pro
NickT
User
Posts: 4
Joined: 26-May-2017
# Posted on: 13-Oct-2017 15:45:26   

daelmo wrote:

CollectionCore.Dispose releases unmanaged and managed resources. IMHO you don't need to worry about that if your EntityCollections go out of scope eventually, which will dispose them automatically. What is your concern about this?

My concern was for memory footprint and memory leaks. Our application does not dispose of these collections in many places. Our object graph for our entities is quite large so entities or their members hanging on to memory is concerning.

Otis wrote:

The Dispose() cleans up eventhandlers which are tied to the entities in the collection. So if you keep an entity around which is in a collection, and you get rid of the collection without removing the entity from that collection then the eventhandlers in the collection bound to the events of the entity will keep the collection around (as the entity event references the handler method of the collection). Dispose will unbind the event handlers of the collection bound to the events of every entity in the collection, so if you keep the entities in that collection around, the collection is not kept in memory because of that. It's an edge case, really. Normally you won't run into that situation that much.

This is very clear. I'll communicate it to our team. Thank you.