X hits on this document

PDF document

“Beginning Visual C# 2005 Express Edition Video Series” - page 52 / 73





52 / 73

Death of an Object

So we understand what happens when we create an object, but what happens when we are finished using an object? This isn't an easy answer, so try to understand what I'm about to say involves some concepts that are rather advanced and I'm not telling you everything.

We sometimes talk about things being in a plastic bubble, maybe you've seen the movie "The Boy in the Plastic Bubble" or watched the Sienfeld episode with the "Bubble Boy". The bubble protects people with weakened immune systems from the outside world. The Bubble is a totally controlled environment. In a like manner, that is what the .NET Runtime does for your applications. It protects your application from the outside world, so that other programs can't corrupt its memory space, and it cleans up after your program when it’s finished. Protecting an application's allocated memory prevents memory leaks or other bugs from shutting down your application immediately. To guard against this, the .NET Runtime has a Garbage Collector, which is a process within the .NET Runtime that searches for object references in your code that are no longer needed. When it finds an unused object it disposes of it, or rather, it removes the object from memory.

As your code is executing, the .NET Runtime will flag those objects that it knows it won't need again, and after a while the Garbage Collector comes through and disposes of all the objects that are flagged for destruction. Right before the Garbage Collector destroys your object, you will get a last second chance to execute some code, which is called a Destructor … I’ll cover that in the next section.

Many developers try to help the .NET Runtime by doing the following:


myCar = null;

Supplemental Readings for the Express Edition Videos Copyright © 2005 LearnVisualStudio.NET


Document info
Document views284
Page views284
Page last viewedTue Jan 24 22:19:06 UTC 2017