30.06.2013 Views

Under the Hood of .NET Memory Management - Simple Talk

Under the Hood of .NET Memory Management - Simple Talk

Under the Hood of .NET Memory Management - Simple Talk

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 1: Prelude<br />

When an Order is added to <strong>the</strong> order collection, <strong>the</strong> collection itself <strong>the</strong>n holds a<br />

reference to <strong>the</strong> added order. If <strong>the</strong> instance <strong>of</strong> <strong>the</strong> Customer class had a stack reference<br />

to it as well, it would have <strong>the</strong> following references:<br />

• a stack-based root reference for a Customer containing:<br />

• a reference to <strong>the</strong> orders ArrayList collection, which contains:<br />

• references to order objects.<br />

Figure 1.6: Reference tree for a typical scenario.<br />

Figure 1.6 shows <strong>the</strong> basic reference tree, with a global root reference to a Customer class<br />

that, in turn, holds a collection <strong>of</strong> Order classes.<br />

This is important because if an object doesn't ultimately have a root reference <strong>the</strong>n it<br />

can't actually be accessed by code, so it is no longer in use, and can be removed. As you<br />

can see above, a large number <strong>of</strong> objects can be maintained by just a single root reference,<br />

which is both good and bad, as we'll see later.<br />

Inspection and collection<br />

To make sure objects which are no longer in use are cleared away, <strong>the</strong> GC simply gets a<br />

list <strong>of</strong> all root references and, for each one, moves along its reference tree "marking" each<br />

object found as being in use (we'll come back to what that means in just a moment). Any<br />

29

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!