Mason11987
Mason11987

Reputation: 330

When using removeall on a list of objects, are the objects removed from memory completely?

I'm having some memory issues on an application I'm setting up that makes frequent use of "removeall" on a list of custom objects. Should something be done to each of the objects to be removed before running this function to clean up any mess?

Thanks!

Upvotes: 2

Views: 421

Answers (2)

Jeff Hornby
Jeff Hornby

Reputation: 13640

RemoveAll will remove them from your collection. If there are any other references to the objects then they will continue to exist.

You could also be using some unmanaged resources that need to be cleaned up. Generally you would put these in a Dispose method if you're implementing IDisposable and also in a Finalize method to ensure that they are cleaned up.

Otherwise, the garbage collector will clean up any managed resources. The only problems this can cause (and this is VERY rare) is that the garbage collector runs at a lower priority and if your processor is seriously pegged, it might not get enough cycles to clean up memory.

Upvotes: 2

phoog
phoog

Reputation: 43046

Generally, no. Let the garbage collector do its work.

If the objects implement IDisposable, you might make a case for calling Dispose on them, but only if you know that they're not being used anywhere else.

If the lists are very large, the lists themselves are probably implicated in your problems. Big lists are a common cause of large object heap fragmentation. In that case, you don't need to concern yourself with the objects contained in the list.

If you describe your memory issues in more detail, you might get a more helpful answer.

Upvotes: 1

Related Questions