#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

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

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 comment