Garbage Collection: Automatic Dynamic Memory Management

Whereas classes are written in the source code before a program is executed, their object counterparts are created dynamically during runtime by using the new operator as described earlier. Every instantiated object occupies a certain amount of memory needed to store items, such as the object's instance variables. Because memory is a scarce resource, it is important to reclaim and thereby recycle the memory representing objects that are no longer used in the program to make space for newly instantiated objects coming into use.

How Do Objects Get Out of Reach?

Before we look at how C# performs this reclaim, it is worth looking closer at what we mean by “an object no longer used.” Briefly ...

Get C# Primer Plus now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.