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.

13<br />

Memory Management<br />

<strong>and</strong> Pointers<br />

WhaT ’ s in This ChaPTer<br />

➤<br />

➤<br />

➤<br />

➤<br />

➤<br />

Allocating space on the stack <strong>and</strong> heap with runtime<br />

Garbage collection<br />

Releasing unmanaged resources using destructors <strong>and</strong> the<br />

System.IDisposable interface<br />

The syntax for using pointers in <strong>C#</strong><br />

Using pointers to implement high - performance stack - based arrays<br />

This chapter presents various aspects of memory management <strong>and</strong> memory access. Although the<br />

runtime takes much of the responsibility for memory management away from the programmer, it is<br />

useful to underst<strong>and</strong> how memory management works <strong>and</strong> important to know how to work with<br />

unmanaged resources effi ciently.<br />

A good underst<strong>and</strong>ing of memory management <strong>and</strong> knowledge of the pointer capabilities provided<br />

by <strong>C#</strong> will better enable you to integrate <strong>C#</strong> code with legacy code <strong>and</strong> perform effi cient memory<br />

manipulation in performance - critical systems.<br />

memory managemenT under The hood<br />

One of the advantages of <strong>C#</strong> programming is that the programmer does not need to worry about<br />

detailed memory management; the garbage collector deals with the problem of memory cleanup on<br />

your behalf. The result is that you get something that approximates the effi ciency of languages such<br />

as C ++ without the complexity of having to h<strong>and</strong>le memory management yourself as you do in C ++ .<br />

However, although you do not have to manage memory manually, it still pays to underst<strong>and</strong> what is<br />

going on behind the scenes. Underst<strong>and</strong>ing how your program manages memory under the covers will<br />

only help you increase the speed <strong>and</strong> performance of your applications. This section looks at what<br />

happens in the computer ’ s memory when you allocate variables.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!