Sign in to follow this  
Eric_B

[.net] ArrayList, Object references, and Objects

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this