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 />

Small Object Heap<br />

Allocation and automatic garbage collection on <strong>the</strong> Small Object Heap (SOH) is quite a<br />

complex process. Because most <strong>of</strong> <strong>the</strong> objects allocated within an application are less than<br />

85 K, <strong>the</strong> SOH is a pretty heavily used storage area. So, to maximize performance, various<br />

optimizations have been added which, ra<strong>the</strong>r unfortunately, add to <strong>the</strong> complexity.<br />

To aid your understanding, I am going to describe <strong>the</strong> SOH at a relatively high level to<br />

start with, and <strong>the</strong>n add <strong>the</strong> complexities slowly until you have <strong>the</strong> full picture. Trust me,<br />

you will thank me for it!<br />

Consecutive allocation on <strong>the</strong> SOH<br />

In unmanaged C/C++ applications, objects are allocated onto <strong>the</strong> heap wherever space<br />

can be found to accommodate <strong>the</strong>m. When an object is destroyed by <strong>the</strong> programmer,<br />

<strong>the</strong> space that that object used on <strong>the</strong> heap is <strong>the</strong>n available to allocate o<strong>the</strong>r objects<br />

onto. The problem is that, over time, <strong>the</strong> heap can become fragmented, with little spaces<br />

left over that aren't usually large enough to use effectively. As a result, <strong>the</strong> heap becomes<br />

larger than necessary, as more memory segments are added so that <strong>the</strong> heap can expand<br />

to accommodate new objects.<br />

Ano<strong>the</strong>r problem is that whenever an allocation takes place (which is <strong>of</strong>ten), it can take<br />

time to find a suitable gap in memory to use.<br />

To minimize allocation time and almost eliminate heap fragmentation, .<strong>NET</strong> allocates<br />

objects consecutively, one on top <strong>of</strong> ano<strong>the</strong>r, and keeps track <strong>of</strong> where to allocate <strong>the</strong><br />

next object.<br />

40

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

Saved successfully!

Ooh no, something went wrong!