Managing the Object Life Cycle

C# is a managed language. Unlike other languages, such as C++, where we need to explicitly manage memory cleanup, in C# we do not need to worry about it. The garbage collector in the .NET Framework manages the allocation and release of memory for us. 

The garbage collector ensures that, as long as we use managed types, that is, value and reference type variables, then we don't have to explicitly destroy an object in order to free its memory. However, as we discovered in Chapter 8, Creating and Using Types in C#, C# also gives us the freedom to utilize the capabilities of pointer object types in it. In C#, we must declare that code using the unsafe syntax. Apart from that, for variables declared in unsafe code, we also need to manage the release of memory.

In this chapter, as well as looking into memory management for unsafe code we will delve into the following topics:

  • The differences between managed and unmanaged code in C#
  • How garbage collection works in C#
  • How a garbage collector uses a managed heap to allocate memory to objects during application execution
  • Understanding the mark-compact algorithm used by the garbage collector
  • How to manage unmanaged resources in C#
  • Understanding finalization and the performance implications of using the finalize method
  • Understanding the IDisposable interface and how it helps overcome the shortcomings of the finalize method
  • Understanding how we can combine the Dispose method with the finalize method to ensure the best performance of our applications
  • Understanding using the using block for all classes that implement the IDisposable interface

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.178.151