#923 – An Object Isn’t Necessarily Deleted as Soon as It’s Dereferenced

An object will typically be deleted, and its memory reclaimed, at some point after the object is no longer being referenced by any reference-typed variables.

The object won’t necessarily be deleted when you stop referencing it.  The garbage collector will only delete the object when it needs to reclaim some space in memory.  We refer to this as nondeterministic destruction–the idea that we can’t predict when an object will be destroyed.

            // myDog references a Dog object
            Dog myDog = new Dog("Kirby");

            // Set reference to null, so that we
            // no longer reference "Kirby" Dog object.
            myDog = null;

            // "Kirby" won't necessarily have been
            // destroyed at this point
Advertisements

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

2 Responses to #923 – An Object Isn’t Necessarily Deleted as Soon as It’s Dereferenced

  1. Pingback: Dew Drop – September 4, 2013 (#1,617) | Alvin Ashcraft's Morning Dew

  2. Agent117 says:

    Reblogged this on Phani Kumar Tadepally.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: