O'Reilly logo

C# Primer: A Practical Approach by Stanley B. Lippman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

1.12. Garbage Collection

We do not explicitly delete objects allocated through the new expression. Rather, these objects are cleaned up by garbage collection in the runtime environment. The garbage collection algorithm recognizes when an object on the managed heap is no longer referenced. That object is marked as available for collection.

When we allocate a reference type on the managed heap, such as the following array object:

int [] fib =
    new int[6]{ 1,1,2,3,5,8 };

the heap object is recognized as having an active reference. In this example, the array object is referred to by fib.

Now let's initialize a second array handle with the object referred to by fib:

int [] notfib = fib;

The result is a shallow copy. Rather than notfib addressing ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required