Introduction

In C#, memory management is handled automatically by a process called garbage collection. This means that you don't need to explicitly allocate or deallocate memory for objects. The garbage collector takes care of this behind the scenes, ensuring that memory is used efficiently.

How Does Garbage Collection Work?

  1. Allocation: When you create an object in C#, the garbage collector allocates memory for it on the heap.
  2. Reference Counting: The garbage collector keeps track of how many references are pointing to an object. If an object has no references, it becomes eligible for collection.
  3. Generation-Based Collection: C# uses a generation-based approach to garbage collection. Objects are divided into generations based on their age. Younger objects are more likely to be short-lived and are collected more frequently. Older objects are less likely to be collected, as they are assumed to be in use.
  4. Collection Process: When the garbage collector detects that memory is running low, it performs a collection cycle. It scans the heap to identify objects that are no longer reachable. These objects are marked for deletion, and their memory is reclaimed.

Example

class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

class Program
{
    static void Main()
    {
        Person person1 = new Person();
        person1.Name = "Alice";
        person1.Age = 30;

        // Reference to person1 is lost
        Person person2 = null;

        // Garbage collector will eventually collect person1
    }
}

In this example, person1 is created and assigned a value. When person2 is set to null, the only reference to person1 is lost. The garbage collector will eventually determine that person1 is no longer reachable and reclaim their memory.

Important Points to Remember

Scenario. Creating and Disposing Objects

Consider the following C# code that creates a disposable object (in this case, a FileStream) and then disposes of it.

C#

using System.IO;

class Program
{
    static void Main()
    {
        using (FileStream fs = new FileStream("example.txt", FileMode.Create))
        {
            // Write data to the file
            byte[] data = new byte[] { 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33 };
            fs.Write(data, 0, data.Length);
        }

        // The FileStream object is automatically disposed of here
    }
}

Use code with caution.

Explanation

  1. Object Creation: A FileStream object is created using the new keyword. This allocates memory for the object on the heap.
  2. Using Statement: The statement is used to ensure that the FileStream object is disposed of properly, even if an exception occurs.
  3. Resource Usage: The FileStream object is used to write data to the file.
  4. Disposal: When the using block exits, the FileStream object's Dispose method is automatically called. This releases any underlying system resources associated with the file, such as file handles.
  5. Garbage Collection: The FileStream object, along with any other objects that are no longer referenced, becomes eligible for garbage collection. The garbage collector will eventually reclaim the memory allocated to these objects.

Key Things

Let me give a very simple example in contrast with daily life to let you understand it better.

Imagine you're cleaning up your room. You throw away trash and put away items you no longer need. This is similar to how garbage collection works in C#. The garbage collector identifies objects that are no longer being used (like trash) and removes them, freeing up space.

Examples

By understanding the basics of garbage collection in C#, you can write more efficient and maintainable code. Remember, the garbage collector is your friend, but it's always good to be aware of how it works.