Wednesday 18 April 2012

Garbage Collection


So far, we have seen that we create objects in C# in exactly the same way that we create objects in other languages, such as C++. We use the new keyword to allocate memory from the heap, and we call a constructor to convert that memory into an object. However, as far as the method for the destruction of objects, there is no similarity between C# and its predecessors.




We Cannot Explicitly Destroy Objects

In many programming languages, we can explicitly control when an object will be destroyed. For example, in C++ we can use a delete expression to deinitialize (or finalize) the object (turn it back into raw memory) and then return the memory to the heap. In C#, there is no way to explicitly destroy objects. In many ways, this restriction is a useful one because programmers often misuse the ability to explicitly destroy objects by:
  • Forgetting to destroy objects.

    If we had the responsibility for writing the code that destroyed an object, we might sometimes forget to write the code. This can happen in C++ code, and this is a problematic bug that causes the user’s computer to get slower as the program uses more memory. This is known as memory leak. Often the only way to reclaim the lost memory is to shut down and then restart the offending program.
  • Attempting to destroy the same object more than once.

    We might sometimes accidentally attempt to destroy the same object more than once. This can happen in C++ code, and it is a serious bug with undefined consequences. The problem is that when we destroy the object the first time, the memory is reclaimed and can be used to create a new object, probably of a completely different class. When we then attempt to destroy the object the second time, the memory refers to a completely different object.
  • Destroying an active object.

    We might sometimes destroy an object that was still being referred to in another part of the program. This is also a serious bug known as the dangling pointer problem, and it also has undefined consequences.

Garbage Collection Destroys Objects for You

In C#, we cannot destroy an object explicitly in code. Instead, C# has garbage collection, which destroys objects for us. Garbage collection is automatic. It ensures that:
  • Objects are destroyed.

    However, garbage collection does not specify exactly when the object will be destroyed.
  • Objects are destroyed only once.

    This means that we cannot get the undefined behavior of double deletion that is possible in C++. This is important because it helps to ensure that a C# program always behaves in a well-defined way.
  • Only unreachable objects are destroyed.

    Garbage collection ensures that an object is never destroyed if another object holds a reference to it. Garbage collection only destroys an object when no other object holds a reference to it. The ability of one object to reach another object through a reference variable is called reachability. Only unreachable objects are destroyed. It is the function of garbage collection to follow all of the object references to determine which objects are reachable and hence, by a process of elimination, to find the remaining unreachable objects. This can be a time-consuming operation, so garbage collection only collects garbage to reclaim unused memory when memory becomes low.

Note

We can force garbage collection explicitly in our code, but it is not recommended. Let the .NET runtime manage memory for us.

0 comments: