CLI is a
managed platform. It means that you don't directly allocate, but, more importantly, you do not control reclaiming memory. In terms of C#, you always allocate managed heap memory with "new" operators and never need to clean up the memory. It is done through the mechanism of
Garbage Collection. Please see:
http://en.wikipedia.org/wiki/Garbage_collection_(computer_science)[
^].
You should also understand that the managed pointers (memory handles, known in C# as references) are complex objects very different from unmanaged pointers. The objects allocated in memory could be automatically moved during run time to by the platform, to optimize memory layout, avoid fragmentation, etc. Any program assuming fixed location of any object in
linear memory (used, for example, in Windows architecture) is incorrect. This is not usually a problem, because most code use memory only through handles.
See also:
http://en.wikipedia.org/wiki/Flat_memory_model[
^].
The advanced feature is using
pointers in managed memory, which only possible in an
unsave
statement, but pointer operations are done through the special process of
pinning of a fragment of memory. In C#, this is done via the
fixed
statement. Please see:
http://msdn.microsoft.com/en-us/library/f58wzh21%28v=vs.100%29.aspx[
^].
Another advanced feature is using the Garbage Collector (CG) API. Please see:
http://msdn.microsoft.com/en-us/library/system.gc.aspx[
^].
I strongly discourage doing it. Garbage collection is very hard to improve controlling it manually; you can only make things worse.
Finally, it's possible to use unmanaged memory in a CLI application indirectly, for example, via P/Invoke. This goes beyond the topic of the question, but the idea is: this is done independently by the unmanaged code itself; your managed code only calls appropriate methods. As a matter or rule, reclaiming of unmanaged resource is done via
System.IDisposable.Dispose
, but this interface is used not only for this purpose. In C#, it's important to use
using
statement whenever possible. Please see:
http://msdn.microsoft.com/en-us/library/system.idisposable%28v=vs.100%29.aspx[
^],
http://msdn.microsoft.com/en-us/library/yh598w02%28v=vs.100%29.aspx[
^].
I would advice to read at least basic introductory documentation before trying to do programming, to avoid total frustration and troubles.
Good luck,
—SA