Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 2. Object Lifetime > Memory Management in the .NET Framework - Pg. 51

Chapter 2 Object Lifetime In this chapter: Memory Management in the .NET Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . .51 Advanced Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .66 An important facet of Microsoft .NET programming is understanding how the Common Language Runtime (CLR) allocates and releases memory and, above all, how objects are destroyed at the end of their life cycles. In this chapter, you'll learn how you can improve your applications' performance remarkably. Note using using using using using To avoid long lines, code samples in this chapter assume that the following using statements are used at the top of each source file: System; System.Collections; System.Diagnostics; System.IO; System.Runtime.InteropServices; Memory Management in the .NET Framework Microsoft Visual C# classes don't have destructor methods. In other words, no method or event in the class fires when the instance is destroyed. This is one of the more controversial features of the framework and was discussed for months in forums and newsgroups while the Microsoft .NET Framework was in beta version. The Garbage Collection Process The .NET Framework memory management relies on a sophisticated process known as garbage collection. When an application tries to allocate memory for a new object and the heap has insufficient free memory, the .NET Framework starts the garbage collection process, which is carried out by an internal object known as the garbage collector. Many technical arti- cles use the GC abbreviation to indicate both the garbage collection and the garbage collector terms; strictly speaking, System.GC stands for garbage collector, even though in most cases it can indicate both terms. The garbage collector visits all the objects in the heap and marks those objects that are pointed to by any variable in the application. (These variables are known as roots because 51