Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am trying to understand how .net CLR work, I listed how it work as far as I know(Maybe there are some mistake, please feel free to point out), and have some questions below.

1, When .Net Compiler compile .net application source code, it will select whatever language compiler.Than creates dll or exe assembly, which includes CIL, Metadata and Manifest.

2, In Manifest, it tells which outside library I need to use. So the library(or other dll) loaded.

3, JIT translate CIL to machine language which the computer can run the application. JIL also check the code safety.

My questions are:
1, Is the CLR run as I mentioned above?

2, I know that the CLR also responsible for Automatic memory management and Garbage Collection. When CLR do those thing?
Posted
Comments
Sergey Alexandrovich Kryukov 2-Jan-13 18:13pm    
This is very rare case when I vote 5 for the question.

I can only repeat that the question are accurately formulated and probably show your intent to get the essence of things. Unfortunately, these days this is quite rare. I answered all your questions so far, and in all three cases I up-voted it with pleasure.

—SA

Let's see:
  1. Pretty much so, but not quite exactly when you write about the JIT operation. Usually, the code is JIT-compiled on per-method basis. When some method is about to be called for the very first time in the lifetime of its assembly, it is JIT-compiled.

    It can be pre-compiled if you use NGEN.EXE, which can be done for some performance benefits, for example: http://msdn.microsoft.com/en-us/library/6t9t5wcf%28v=vs.110%29.aspx[^].

    This is a very interesting in-depth CodeProject article on JIT optimization: JIT Optimizations[^].
  2. Memory management is and garbage collection work pretty much all the time; their operation is independent from the application process and transparent to it. The general rule is: you can never rely on any particular order of operation.

    You can prevent memory management operations when you pin some memory. This is done by using fixed statement. Please see:
    http://msdn.microsoft.com/en-us/library/f58wzh21.aspx[^].

    There are a number of cases when you really need pinning. Normally, you do it only if you directly use pointers in an assembly compiled with unsafe option in the code under unsafe statement: http://msdn.microsoft.com/en-us/library/chfa2zb8%28v=vs.110%29.aspx[^].

    Again, this is an interesting in-depth CodeProject article on pinning: Pinned Object[^].

    And now, about GC. The operation of GC is driven by the reachability of objects. The actual call of the object destructors and the act of reclaiming memory cannot be predicted bu the application (by the way, this is the reason why destructores are rarely written in the typical .NET applications, usually this is not needed and can created some problems of race conditions type), but it happens when some object becomes unreachable.

    By the way, this is not so trivial criterion as you might think. For example, if object A references object B, object B references object C, and C reference A, this cyclic dependency does not prevent GC from figuring out that all objects should be garbage-collected if there are no other referenced to them. This is easy to check up by a simple experiment.

    You can find the detail of reachability and garbage collection here: http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29[^].


Good luck,
—SA
 
Share this answer
 
v5
Comments
Andreas Gieriet 2-Jan-13 18:22pm    
Excellent answer!
My 5!
Andi
Sergey Alexandrovich Kryukov 2-Jan-13 18:25pm    
Thank you, Andi.
—SA
Chelseajcole 3-Jan-13 10:31am    
Thank You Very Much for The Answer!! And Happy New Year!
Sergey Alexandrovich Kryukov 3-Jan-13 11:26am    
You are very welcome.
Happy New Year!
Call again,
—SA
Memory management happens when it feels like it. This is not good enough if you use large objects in memory, like videos. It's fine for most applications.

It has nothing to do with the compilation process that you listed. It's something else entirely.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900