Explain how the .NET Framework performs automatic memory management.Automatic memory management involves two major steps:
1. Allocation of memory 2. Release of memory
Allocation of memory - When a new process is initialized, runtime reserves address space in memory for the process. This address space inside the memory is known as heap. At the very initial stage, the pointer is at the base address of managed heap. When the application creates an object, the garbage collector allocates memory in the address space immediately following the first object. As long as address space is available, the GC keeps allocating space to the objects one over another. Runtime allocates memory to an object by adding a value to a pointer pointing to previously allocated object. This makes it faster.
Releasing memory - Garbage collector figures out the best time to perform collection. This is done on the basis of how memory was allocated. GC releases all the unused objects. To find which ones are not in use, GC examines the application’s roots. All the application roots are assigned to the objects or are set to null. A graph is formed on the basis of this and the objects which are not part of this graph are released from the memory.
|