15.02.2015 Views

C# 4 and .NET 4

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

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

Memory Management Under the Hood ❘ 309<br />

The following code instructs the compiler that you need space in memory to store an integer <strong>and</strong> a double,<br />

<strong>and</strong> these memory locations are referred to as nRacingCars <strong>and</strong> engineSize. The line that declares each<br />

variable indicates the point at which you will start requiring access to this variable. The closing curly brace<br />

of the block in which the variables are declared identifies the point at which both variables go out of scope:<br />

{<br />

}<br />

int nRacingCars = 10;<br />

double engineSize = 3000.0;<br />

// do calculations;<br />

Assuming that you use the stack shown in Figure 13-1, when the variable nRacingCars comes into scope<br />

<strong>and</strong> is assigned the value 10, the value 10 is placed in locations 799996 through 799999, the 4 bytes just<br />

below the location pointed to by the stack pointer (four bytes because that’s how much memory is needed to<br />

store an int.) To accommodate this, 4 is subtracted from the value of the stack pointer, so it now points<br />

to the location 799996, just after the new first free location (799995).<br />

The next line of code declares the variable engineSize (a double) <strong>and</strong> initializes it to the value 3000.0. A<br />

double occupies 8 bytes, so the value 3000.0 is placed in locations 799988 through 799995 on the stack,<br />

<strong>and</strong> the stack pointer is decremented by 8, so that once again, it points to the location just after the next free<br />

location on the stack.<br />

When engineSize goes out of scope, the computer knows that it is no longer needed. Because of the way<br />

variable lifetimes are always nested, you can guarantee that, whatever happened while engineSize was in<br />

scope, the stack pointer is now pointing to the location where engineSize is stored. To remove engineSize<br />

from the stack, the stack pointer is incremented by 8 <strong>and</strong> it now points to the location immediately after<br />

the end of engineSize. At this point in the code, you are at the closing curly brace, so nRacingCars also<br />

goes out of scope. The stack pointer is incremented by 4. When another variable comes into scope after<br />

engineSize <strong>and</strong> nRacingCars have been removed from the stack, it overwrites the memory descending<br />

from location 799999, where nRacingCars was stored.<br />

If the compiler hits a line such as int i, j, then the order of variables coming into scope looks<br />

indeterminate. Both variables are declared at the same time <strong>and</strong> go out of scope at the same time. In this<br />

situation, it does not matter in what order the two variables are removed from memory. The compiler<br />

internally always ensures that the one that was put in memory first is removed last, thus preserving the rule<br />

about no crossover of variable lifetimes.<br />

reference data Types<br />

Although the stack gives very high performance, it is not flexible enough to be used for all variables. The<br />

requirement that the lifetimes of variables must be nested is too restrictive for many purposes. Often, you<br />

need to use a method to allocate memory for storing data <strong>and</strong> keeping that data available long after that<br />

method has exited. This possibility exists whenever storage space is requested with the new operator — as is<br />

the case for all reference types. That is where the managed heap comes in.<br />

If you have done any C++ coding that required low-level memory management, you are familiar with the<br />

heap. The managed heap is not quite the same as the heap C++ uses; the managed heap works under<br />

the control of the garbage collector <strong>and</strong> provides significant benefits when compared to traditional heaps.<br />

The managed heap (or heap for short) is just another area of memory from the processor’s available<br />

4GB. The following code demonstrates how the heap works <strong>and</strong> how memory is allocated for reference<br />

data types:<br />

void DoWork()<br />

{<br />

Customer arabel;<br />

arabel = new Customer();<br />

Customer otherCustomer2 = new EnhancedCustomer();<br />

}<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!