 grabs onto the second. y = z; // Finally, x lets go of the first Student object, and grabs onto // the second, as well; the first Student object is now lost to // the program because we no longer have any reference variables // maintaining a "handle" on it! x = z;
As it turns out, if all of an object's handles are lost, it might seem as though the memory that the object occupies is permanently wasted. (In a language like C++, this is indeed the case, and programmers have to explicitly take care to "reclaim" the memory of an object that is no longer needed before all of its handles are dropped. Failure to do so is a chronic source of problems in C++ programs.) In C# (and all other .NET languages) the common language runtime (CLR) periodically performs garbage collection, a process that automatically reclaims the memory of "lost" objects for us. We'll revisit this topic in Chapter 13.