Click here to Skip to main content
11,478,174 members (72,438 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: .NET
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 2-Jan-13 12:45pm
Comments
Sergey Alexandrovich Kryukov at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
v5
Comments
Andreas Gieriet at 2-Jan-13 18:22pm
   
Excellent answer!
My 5!
Andi
Sergey Alexandrovich Kryukov at 2-Jan-13 18:25pm
   
Thank you, Andi.
—SA
Chelseajcole at 3-Jan-13 10:31am
   
Thank You Very Much for The Answer!! And Happy New Year!
Sergey Alexandrovich Kryukov at 3-Jan-13 11:26am
   
You are very welcome.
Happy New Year!
Call again,
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 229
1 F-ES Sitecore 170
2 Sascha Lefèvre 160
3 Frankie-C 120
4 OriginalGriff 95
0 Sergey Alexandrovich Kryukov 7,675
1 OriginalGriff 7,321
2 Sascha Lefèvre 3,034
3 Maciej Los 2,491
4 Richard Deeming 2,325


Advertise | Privacy | Mobile
Web02 | 2.8.150520.1 | Last Updated 2 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100