Memory Lifecycle in .NET – An Overview

Memory 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 doesn’t have any value. The allocated memory is initialized with a default value in this part of lifecycle. Consider the following statement:
testObj = new testClass( );

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
{
private testClass testObj;

public testMemoryAccess(){ }
public void useTestObj()
{
testObj = new testClass();
testObj.callMethod();
testObj = null;
}

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 object’s finalize method as below.

public class testGC
{
public testGC()
{
}

protected override void Finalize()
{
//close all open files, network connections and other unmanaged resources
}
}

| Additional Ways of Ensuring Security in .NET | Memory Lifecycle in .NET – An Overview | Few Best Coding Practices for ASP .NET | Handling Session Efficiently Using SQLSERVER State Management in .NET | How to Restrict a Program to Single Instance in .NET? | How to Use Structured Exception Handling in .NET | Understanding Boxing Versus Unboxing in .NET | Understanding Different Levels of Security in .NET | Understanding the Disadvantages of Memory Management in .NET | Using Membership API for Secured Coding in .NET |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.