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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 2: The <strong>Simple</strong> Heap Model<br />

GC operation also depends on whe<strong>the</strong>r <strong>the</strong> application is server- or workstation-based,<br />

and on its latency mode. To avoid confusion at this stage, let's go through <strong>the</strong> general<br />

principles <strong>of</strong> generational garbage collection first, and <strong>the</strong>n we will cover <strong>the</strong>se more<br />

advanced topics in <strong>the</strong> next chapter.<br />

So now let's take a look at what happens when a GC is triggered from each <strong>of</strong><br />

<strong>the</strong> generations.<br />

Gen 0 collection<br />

Gen 0 objects have never been looked at by <strong>the</strong> GC before, so <strong>the</strong>y have two possible<br />

options when <strong>the</strong>y are finally inspected:<br />

• move to Gen 1 (if <strong>the</strong>y have a root reference in <strong>the</strong>ir hierarchy)<br />

• die and be compacted (if <strong>the</strong>y are rootless and <strong>the</strong>refore no longer in use).<br />

Ei<strong>the</strong>r way, <strong>the</strong> result <strong>of</strong> a Gen 0 GC is an empty Gen 0, with all rooted objects in Gen 0<br />

being copied to (and reclassified as) Gen 1, joining <strong>the</strong> o<strong>the</strong>r Gen 1 objects. Remember, in a<br />

Gen 0 collection, existing Gen 1 objects stay where <strong>the</strong>y are.<br />

Figure 2.5 shows <strong>the</strong> state <strong>of</strong> <strong>the</strong> SOH after a GC on <strong>the</strong> heap (as previously displayed in<br />

Figure 2.4 ). Notice how Object Y has now joined Object X in Gen 1, leaving Gen 0 empty.<br />

Object Z didn't have a root reference, and so will be effectively overwritten by <strong>the</strong> next<br />

allocation, because <strong>the</strong> NOP is simply repositioned below its heap location.<br />

After a GC, Gen 0 will always be empty.<br />

46

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

Saved successfully!

Ooh no, something went wrong!