About Memory Management in .NET

2011-04-07


Today I give a topic which maybe most of us are interested in it, no matter you are a C++ or C# programmer, or even you are not a programmer, it is from the following questions which most of us always asked:

1: Why theres is no more Pointer in C#? 2: How to release memory in C# if no Pointer ? 3: Can we force to release memory in C# ?

I am not a good article writer and can not speak too much in English. So I try to give you guys simple concepts and simple content:

There is really big differences between C# and C++ about Memory Management.

What C++ did:

In C++, when we allocate a memory to a object or class, we normally have to free it (release) otherwise this memory will NOT released and can not be used by others applications, this is called "Memory Leak", a professional C++ programmer should always manage his memory related code otherwise your application will "Down" and it might "Down" your windows system also. So Pointer in C++ is powerful and flexible but you have to accept it is also very dangerous . ** Manged code and Unmanaged code**

In .NET (or Java) , pointer has been removed (you still can use Pointer in C#, actually it is that you embedded your C++ code in C# and indicate C++ code using "Unsafe" word , they are still C++ code ); People call C# and Java Managed Code, which means some important things have been "Managed" by .NET framework (Java has its framework too), then you know now we call C++ is Unmanaged code.

The one of "Managed" thing is Memory management.

the programming machination changed a lots in C#, The big changes in C# (here we should call .NET, here I use C# instead of .NET ) is that programmer does NOT need to manage memory which he used, well, actually you can say you CAN NOT manage it . Because the memory allocate and memory release are Managed by .NET .

Garbage Collection:

.NET framework uses AppDomain to give each application, different applications has different AppDomain, .NET manage AppDomain to guarantee each applications can not occupy memory which other applications are using. All memory is managed by a "Model" which is called Garbage Collection (we can GC) in .NET.

GC has its mechanism to collect "Garbage Memory" (Garbage Memory is the memories unit are no more used by all applications), programmer can load GC.Collect() method to try to "force release memory" but the force will not work as your understanding, and load GC.Collect() is not recommended because it leads unexpected issues.

GC is always running in .NET, it ONLY collect garbages memory under some conditions, for example, here is a condition: when it found the available memory is not enough, it will try to collection all garbage memory and let them to be reused.

Here we have to know:

1: No matter we use some code to try to force release memory (for example, dispose or even load GC.Collect() method), The GC will NOT work at once, your memory will NOT released at once either, ONLY GC found the "Garbage Collection Time " comes, then it works to collect garbage memory;

2: Do not worry about GC occupy system time, this is totally a wrong understanding. how can you control GC to stop working ? and you never know what time GC will work. (btw: there is no more destruct concept in .NET even you can use it in C#, you only can use Finalize , but Finalize is not Release Memory)

**Summary

1: Clarify concept again: Dispose NOT equals Release memory, Finalize NOT equals memory either, there is not a Right Away way to release memory in C#, and actually we DO Not need to concern it because GC is there;

2: ONLY GC can release your memory in C# ; (sure you still can use C++ code to release memory right away)

3: GC is always there, it will NOT effect your application time because it tries to avoid your application time, ONLY it found it has to release memory, or when it found system resource is not enough, or other conditions due to GC's mechanism;

4: When you use "new" to alloc a local object in a method, when this method end, the memory of the object will be set to wait to be garbage collect, but maybe a good coding habit is setting this object = null or you can load dispose it, actually due to 1., you can not release the memory at once; ** About more detail GC, you guys can search on Google, Here is a article from Microsoft below: http://msdn.microsoft.com/en-us/magazine/bb985010.aspx