Chapter 7. Memory Management and Pointers
This chapter looks at various aspects of memory management and memory access. Although the runtime takes much of the responsibility for memory management away from the programmer, it is useful to understand how memory management works and important to know how to work with unmanaged resources efficiently.
If you have a good understanding of memory management and knowledge of the pointer capabilities provided by C#, you are also better positioned to integrate C# code with legacy code and perform efficient memory manipulation in performance-critical systems.
Specifically, this chapter discusses:
How the runtime allocates space on the stack and the heap
How garbage collection works
How to use destructors and the
System.IDisposable
interface to ensure unmanaged resources are released correctlyThe syntax for using pointers in C#
How to use pointers to implement high-performance stack-based arrays
Memory Management under the Hood
One of the advantages of C# programming is that the programmer doesn't need to worry about detailed memory management; in particular, the garbage collector deals with the problem of memory cleanup on your behalf. The result is that you get something that approximates the efficiency of languages like C++ without the complexity of having to handle memory management yourself as you do in C++. However, although you don't have to manage memory manually, if you need to write efficient code, it still pays to understand what is going ...
Get Professional C# 2005 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.