Memory Lifecycle in .NET An OverviewMemory is a critical resource in any application you develop. It has a major impact on the performance and cost of an application. The entire .NET framework runs inside the runtime engine of CLR. This CLR provides automatic memory management and garbage collection for a .NET application.
With these benefits known, browse through this article to know the steps involved in Memory Lifecycle, though few of these steps happen automatically.
More details about each of the phase in memory lifecycle is discussed below: Allocate Memory for a Resource: CLR allocates space from the managed heap for the resource. When your program starts, managed heap will be empty. First Object that is instantiated will be allocated to the address in 0th position. Next Object is stored in 1st position and so on. The address in which next object can be allocated will be pointed by pointer termed NextObjPtr which is an internal reference pointer used by CLR. If the managed heap is already full, then OutOfMemoryException is thrown when initializing the Object. Assuming that a program has instantiations of Object A1, Object A2,Object A3 then the managed heap will have the objects in the below mentioned order and the NextObjPtr will be pointing to the next available address. Each Object will occupy necessary bytes. This diagram below represents only the order of storage in the heap and not the bytes occupied.
Initialize
Memory: The memory that is allocated to the object doesnt have
any value. The allocated memory is initialized with a default value in
this part of lifecycle. Consider the following statement: Does this statement take care of Memory Initialization? The answer is No. Instantiating an object using new operator allocates the memory but no value is assigned to it. After creating the object, the compiler generates a call to the constructor of that Object automatically. Initialization happens during this call. Access Memory: When the Object is active and does some action, memory is being accessed to store the corresponding value. public class
testMemoryAccess public testMemoryAccess(){
} You can explicitly specify that the object is out-of-scope by assigning null value to it. However with managed resources, this is not necessary. Garbage Collector will automatically detect the objects that are no longer in use and clean them up. Free Memory:
Since memory is allocated from managed heap, you need not release the
memory explicitly in your code. It is automatically taken care by Garbage
Collection Algorithm. This algorithm runs periodically or when there is
no space in the managed heap. You can also force garbage collection to
happen by including System.GC.Collect() in your code. However, if there
are any unmanaged resources used then you should explicitly clean them
before garbage collector picks your object for clean up. Few examples
for unmanaged resources are files and network connection. To perform the
explicit cleanup activity, you have to override the objects finalize
method as below. protected
override void Finalize()
|