3.10 Memory Management and the CLR

 < Day Day Up > 



Automatic memory management is a service that you get with the CLR during managed execution. The CLR garbage collector will oversee the allocation and deallocation of memory, saving the developer having to write acres of memory management code, possibly getting it wrong, and crashing the application.

When a new process is started, the CLR puts aside a chunk of memory, which is called the managed heap. The managed heap contains a pointer, initially set at the base address, to the address where the next object in the heap will be allocated. All reference types are allocated on the managed heap, and the CLR will automatically allocate address space from the heap as long as there are enough addresses in the address space.

This process is extremely fast since the CLR manages objects consecutively in a contiguous block in the heap.

Garbage collection works by deciding the best time to take back some memory. By working in a series of generations, the garbage collector determines how recently an object has been used. When the first generation (actually generation 0) is full it will then perform what is known as a collection and release memory held by objects that are no longer being used.

Memory-collection decisions are based on an application's roots, which either contain a null or a reference to an object. An internal graph is created of objects that can be reached from these roots: any object that does not figure on this graph is considered unreachable, and its memory will be deallocated. These fragments of memory are then compacted into contiguous blocks, and the application roots are updated so that they point to the object in its new location.

Some applications, known as unmanaged, require explicit clean up of the memory. This is mostly when the unmanaged resource is wrapping around an operating system resource such as a file handle or network connection. The developer will then need to clean-up this code using clean up methods.



 < Day Day Up > 



Microsoft  .NET. Jumpstart for Systems Administrators and Developers
Microsoft .NET: Jumpstart for Systems Administrators and Developers (Communications (Digital Press))
ISBN: 1555582850
EAN: 2147483647
Year: 2003
Pages: 136
Authors: Nigel Stanley

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net