• Advertisement
Sign in to follow this  

[.net] ArrayList, Object references, and Objects

This topic is 4872 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I've an arraylist to hold the references for objects. When I set it to clear, all the references to the objects are set to null or do I've to loop through the list, set each reference to null, and then clear the list? How do I make sure that these referenced objects are not left in the memory?

Share this post


Link to post
Share on other sites
Advertisement
When you call Clear() all references in the collection are removed. If there is still a remaining reference to an object that used to be in the collection that object will not be removed from memory otherwise that reference would become invalid.

The only way to make sure is that you should always clean up references that you don't need any more. The only exception is a local reference; these will be eliminated by the 'normal stack behaviour'.

The garbage collector will clean up when needed. You could keep track of this collection process by adding a destructor/finalizer to your class and write something to a log in that method. The destructor/finalizer will be called on garbage collection but will cost extra processing time...

Cheers

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement