Calling garbage collection

Although it's not recommended, and we hardly find any reason or circumstance where we seldom need to call the garbage collector explicitly during the program execution, we can use the following syntax to execute the Collect method in garbage collection. The following is the code implementation for this:

GC.Collect();
GC.WaitForPendingFinalizers();

GC is present in the system namespace. The Collect method executes the mark-compact algorithm, which we discussed in the previous section. The WaitForPendingFinalizers method pauses or suspends the current thread until the garbage collector finishes its execution. 

Now that we have a fair understanding of how garbage collection works in C#, we will look at how we can perform memory management for unmanaged objects or unmanaged code.

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

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