Have an idea about CLR................

Description

See this to learn a bit about CLR.

Comments
Would you like to comment?

Sign In if already a member, or Join Now for a free account.

Presentation Transcript Presentation Transcript

Inside the guts of the CLR : Inside the guts of the CLR Kumar Gaurav Khanna gkhanna@microsoft.com Developer Evangelist Microsoft Corporation Understanding how .NET CLR works

Getting to know the CLR a little better… : Getting to know the CLR a little better… Demystifying managed code execution (aka Hosting the CLR) Collected your garbage? (or understanding the GC better) Weak references… anyone? (or how to reuse the managed heap) Peeking at the future of CLR (or taking a look at what’s inside Whidbey CLR)

Demystifying managed code execution (aka Hosting the CLR) : Demystifying managed code execution (aka Hosting the CLR)

Microsoft .NET Execution Model : VB Source code Compiler C++ C# Compiler Compiler Assembly IL Code Assembly IL Code Assembly IL Code Operating System Services Common Language Runtime JIT Compiler Native Code Managed code Microsoft .NET Execution Model COBOL Compiler Assembly IL Code

Question: Is MSCOREE the CLR? : Question: Is MSCOREE the CLR?

Assembly Execution : Assembly Execution PE Header Unmanaged Stub Metadata Common Language Runtime

Slide7 : The Virtual Execution System

What happens when you double-click a managed executable? : What happens when you double-click a managed executable? Stub loads correct CLR via MSCOREE CLR is pointed to managed code for execution _CorExeMain and _CorDllMain are the managed code entry points JIT is invoked on-demand and native code is executed

But let’s face the facts… : But let’s face the facts… OS understands only unmanaged code and Win32 Process .NET Compilers produce only PE files IL needs to be executed in AppDomain OS doesn’t understand AppDomain However, CLR understands all this!

Load the CLR: Introducing CorBindToRuntimeEx : Load the CLR: Introducing CorBindToRuntimeEx Specify the CLR version Specify the CLR Type – Server or Workstation Loading Flags – eg. Concurrent GC, Single Domain, Multi Domain, etc CoClass ID of CorRuntimeHost Interface ID of CorRuntimeHost Returns interface pointer of ICorRuntimeHost

Slide11 : Win32 Process Common Language Runtime JIT Compiler Native Code Inside CorBindToRuntimeEx Via CorBindToRuntimeEx

Load the CLR: Exemplification : Load the CLR: Exemplification LPWSTR pszVer = “v1.0.1216”; ICorRuntimeHost *pHost = NULL; hr = CorBindToRuntimeEx(pszVer, NULL, NULL, CLSID_CorRuntimeHost, IID_ICorRuntimeHost, (void **)&pHost); if (SUCCEEDED(hr)) { }

And ICorRuntimeHost… : And ICorRuntimeHost… Allows transitions from unmanaged world to managed world ICorRuntimeHost::GetDefaultAppDomain User loaded AppDomain

So, are you thinking how we get to host the CLR? : So, are you thinking how we get to host the CLR?

If yes, then here’s how… : If yes, then here’s how… Create the managed code that you need to execute Write the unmanaged client to host the CLR For VC++, #include For VB, reference mscoree TLB Create a CCW proxy for the managed code and reference it in your client CorBindToRuntimeEx and ICorRuntimeHost to execute the managed code

Slide16 : Inside Hosting the CLR

Collected your garbage? (or understanding the GC better) : Collected your garbage? (or understanding the GC better)

Why Garbage Collection? : Why Garbage Collection? Helps you not leak memory System knows when and how to managed memory better Optimize memory allocation for objects Large object heap and regular heap Helps you not touch memory that has been freed

Memory Allocation Basics : Memory Allocation Basics And continuous allocation results in this…

GC Reference Tracing : GC Reference Tracing Application Roots are the starting points Global and static object pointers Local object pointers on a thread stack Any object referenced by a root is termed reachable; anything else is garbage! When GCed

The collection process : The collection process New object allocation initiates the GC Initially all objects are regarded garbage Application Roots are traced; traceable objects are marked as reachable Once trace is over, garbage object memory is released Fragmented heap is compacted; LOH is not compacted Instruction for object memory allocation is restarted

Garbage Collection Memory Allocation : Garbage Collection Memory Allocation Managed Heap

Garbage Collection Mark and Sweep : Roots (strong references) Globals Statics Locals CPU Registers Garbage Collection Mark and Sweep

Garbage Collection Generations : Garbage Collection Generations Roots (strong references)

Garbage Collection Generations : Garbage Collection Generations Roots (strong references)

Garbage Collection Generations : Garbage Collection Generations Roots (strong references)

More GC Trivia : More GC Trivia CLR Garbage Collector is multi-generational, mark-and-sweep It is self-tuning and highly optimized Performance implications of garbage collected memory management are not you expect Allocations are blazingly fast, much faster than the CRT Reliable locality of reference The CLR takes advantage of L1 and L2 caches Garbage collection cost Generation 0 GC cost similar to that of a page fault 0 to 10 milliseconds for Generation 0 10 to 30 milliseconds for Generation 1 Separate workstation and server Garbage Collectors Always use Dispose/Close

Slide28 : GC Demo

Collected or Finalized? : Collected or Finalized? Objects implementing a destructor (aka Finalize method) are Finalizable objects Before object is collected, the Finalize method is invoked public class BaseObj { public BaseObj() { } protected override void Finalize() { // Perform resource cleanup code here... // Example: Close file/Close network connection Console.WriteLine("In Finalize."); } }

Finalization is not good… : Finalization is not good… Finalizable objects take longer to allocate They are promoted to older generation and thus, increase memory pressure Finalize method execution is non-deterministic Finalize executes on GC’s thread, not your application’s thread! No order of Finalize method invocation by the GC

How Finalization Works : How Finalization Works Objects that implement Finalize Queue of objects that have been collected and who Finalize needs to be called

How Finalization Works - II : How Finalization Works - II After 1st GC After 2nd GC

Slide33 : Finalizers execute in different thread

Weak References : Weak References

Weak References : Weak References

Slide36 : Working with Weak References

Peeking at the future of CLR (or taking a look at what’s inside Whidbey CLR) : Peeking at the future of CLR (or taking a look at what’s inside Whidbey CLR)

RAD Debugging : RAD Debugging Edit and Continue: Edit Code at runtime Allowed Edits: Examples Add private fields to a class Add private non-virtual methods to a class Change a function body, even while stepping Disallowed Edits: Examples Removing fields/methods Edits to generic classes Serialization will not recognize new fields Display Attributes for a better debugging experience

The Big Picture : Frameworks (BCL, WinFX, etc.) The Big Picture Compiler EXE (IL) JIT GC Loader Metadata Debugging Interop Remoting Exceptions Debugger Threading Security CLR IDE Editor Source Files

EnC – Editing a Method (1) Delta IL & Delta Metadata : EnC – Editing a Method (1) Delta IL & Delta Metadata Full Metadata (In Running Process) Delta Metadata (Provided by debugger) Delta IL 0 4 2050 20a8 8940 8944

EnC – Editing a Method (2) Remap : EnC – Editing a Method (2) Remap .locals init (int32, int32) . . . call Console.ReadLine pop nop ret .locals init (int32, int32, double) . . . call Console.ReadLine pop . . . call Console.ReadLine pop nop ret Original IL New IL Variables IL Virtual Execution System . . . call FFFFD630 pop ebx pop esi pop edi . . . Original x86 x86 Processor . . . call FFFFD630 fild dword … fstp dword … mov ecx, edi . . . call FFFFD630 pop ebx pop esi pop edi New x86

Edit and Continue… : Edit and Continue…

Slide43 : Edit and Continue

Generics : public class List { private object[] elements; private int count; public void Add(object element) { if (count == elements.Length) Resize(count * 2); elements[count++] = element; } public object this[int index] { get { return elements[index]; } set { elements[index] = value; } } public int Count { get { return count; } } } Generics public class List { private T[] elements; private int count; public void Add(T element) { if (count == elements.Length) Resize(count * 2); elements[count++] = element; } public T this[int index] { get { return elements[index]; } set { elements[index] = value; } } public int Count { get { return count; } } } List intList = new List(); intList.Add(1); intList.Add(2); intList.Add(“3"); int i = (int)intList[0]; List intList = new List(); intList.Add(1); // Argument is boxed intList.Add(2); // Argument is boxed intList.Add(“3"); // Should be an error int i = (int)intList[0]; // Cast required List intList = new List(); intList.Add(1); // No boxing intList.Add(2); // No boxing intList.Add(“3"); // Compile-time error int i = intList[0]; // No cast required

Generics : Generics Why generics? Compile-time type checking Performance (no boxing, no downcasts) Reduced code bloat (typed collections) VB, C#, MC++ produce & consume generics Use generics freely in internal APIs Consider using generics in public APIs Generics are not yet in CLS To be CLS compliant, provide a non-generic API alternative Microsoft is actively pursuing standardization of generics in runtime and languages

Generics in VB : Generics in VB Public Class List(Of ItemType)      Private elements() As ItemType      Private elementcount As Integer      Public Sub Add(ByVal element As ItemType)           If elementcount = elements.Length Then Resize(elementcount * 2)           elements(elementcount) = element           count += 1      End Sub      Public Default Property Item(ByVal index As Integer) As ItemType           Get               Return elements(index)           End Get           Set (ByVal Value As ItemType)               elements(index) = Value           End Set      End Property      Public ReadOnly Property Count As Integer           Get               Return elementcount           End Get      End Property End Class Dim intList As New List(Of Integer) intList.Add(1)           ‘ No boxing intList.Add(2)           ‘ No boxing intList.Add(“3")     ‘ Compile-time error Dim i As Integer = intList(0)  ’ No cast required

Generics in C++ : Generics in C++ generic public ref class List {    array^ elements;    int count; public:    void Add(T element) {       if (count == elements->Length) Resize(count * 2);       elements[count++] = element;    }    property T default [int index] {       T get() { return elements[index]; }       void set(T value) { elements[index] = value; }    }    property int Count {       int get() { return count; }    } }; List^ intList = gcnew List(); intList->Add(1);           // No boxing intList->Add(2);           // No boxing intList->Add(“3");     // Compile-time error   int i = intList[0];        // No cast required

Generics In IL : Generics In IL

Slide49 : Generics Performance

Summary… : Summary… Getting to the CLR better helps build better applications GC should be understood well to write more performant applications Weak References aid in reducing expensive heap operations; use them With each version of the .NET Framework, CLR is getting more advanced and optimized

For More Information… : For More Information… This PPT and Source Code Download at http://www.microsoft.com/india/msdn/events/presentations.aspx MSDN Web site at msdn.microsoft.com

Inspired Community; Inspiring People!! : Inspired Community; Inspiring People!! Recognition program – strives to identify budding dedicated individuals who share a passion for community Found active in newsgroups, online forums, usergroups, inventing new ways to help their peers For more information visit: http://www.microsoft.com/india/communitystar/ Microsoft MVPs are Globally recognized experts, awarded by Microsoft for voluntarily sharing their technical expertise and supporting the community Credible Reliable For more information, visit: http://www.microsoft.com/india/mvp/

Usergroups : Usergroups Exist to facilitate education and peer to peer knowledge exchange Run by the community, for the community Great place to Learn about new technologies Discuss problems Interact with peers Interested? Join one today! http://www.microsoft.com/india/msdn/usergroups/

MSDN Expert Chats : MSDN Expert Chats Talk to experts on the upcoming technologies Get your queries cleared Understand how the technologies should be used optimally http://www.microsoft.com/india/msdn/chat/

Questions? gkhanna@microsoft.com http://www.wintoolzone.com/ http://geekswithblogs.net/khanna/ : Questions? gkhanna@microsoft.com http://www.wintoolzone.com/ http://geekswithblogs.net/khanna/

Slide56 : © 2001 Microsoft Corporation. All rights reserved.

Copyrights © 2009 authorGEN. All rights reserved.