Introducing the Common Language Runtime. The Common Language Runtime The Common Language Runtime...

16
Introducing the Common Introducing the Common Language Runtime Language Runtime
  • date post

    22-Dec-2015
  • Category

    Documents

  • view

    254
  • download

    1

Transcript of Introducing the Common Language Runtime. The Common Language Runtime The Common Language Runtime...

Introducing the Common Introducing the Common Language RuntimeLanguage Runtime

The Common Language The Common Language RuntimeRuntime

• The Common Language Runtime (CLR)The Common Language Runtime (CLR)– Execution engine of The .NET FrameworkExecution engine of The .NET Framework– Offers many system servicesOffers many system services– Similar to an OSSimilar to an OS– Runs on a host OSRuns on a host OS

• The .NET FrameworkThe .NET Framework– New platform by Microsoft New platform by Microsoft – Designed to target the InternetDesigned to target the Internet

The Purpose of the CLR…The Purpose of the CLR…

• Safe binary executionSafe binary execution– Security, memory protectionSecurity, memory protection– Running un-trusted code, locallyRunning un-trusted code, locally

• Performance and FunctionalityPerformance and Functionality– Native execution (JIT Compiled)Native execution (JIT Compiled)– No SandboxNo Sandbox

……The Purpose of the CLRThe Purpose of the CLR

• Bug reductionBug reduction– More code interacting, bugs affect more More code interacting, bugs affect more

usersusers

• Ease of integrationEase of integration

• Developer investmentDeveloper investment– Reuse skills from project to project, even Reuse skills from project to project, even

if the projects are vastly differentif the projects are vastly different

Managed Code and the Managed Code and the CLRCLR• The Common Language Runtime (CLR) The Common Language Runtime (CLR)

is a runtime engineis a runtime engine– Manages .NET Code (such as C# Manages .NET Code (such as C#

applications)applications)– Provides features such as memory Provides features such as memory

management, thread management, object management, thread management, object type safety, security, etc.type safety, security, etc.

– Is a part of the .NET FrameworkIs a part of the .NET Framework

Windows (or other operating oystem)

Common Language Runtime(JIT compilation, memory management, etc.)

Legacy Software

(unmanaged code)

Managed ExecutableReusable

Managed Components

The CLR and Managed The CLR and Managed CodeCode

What is Managed CodeWhat is Managed Code

• Code that targets the CLRCode that targets the CLR– C#, VB.NET, PERL, Java, C++, Cobol, etc.C#, VB.NET, PERL, Java, C++, Cobol, etc.

• All Managed code has all features of CLRAll Managed code has all features of CLR– Object oriented infrastructureObject oriented infrastructure– Internet oriented infrastructureInternet oriented infrastructure– Access to the Framework Class Library (FCL)Access to the Framework Class Library (FCL)

• All Managed executables consist ofAll Managed executables consist of– Intermediate Language (IL) instructionsIntermediate Language (IL) instructions– MetadataMetadata

IL and MetadataIL and Metadata

• IL IL – CPU independent machine languageCPU independent machine language– Just-in-time compiled at runtimeJust-in-time compiled at runtime– Makes cross-language integration Makes cross-language integration

possible. possible.

• MetadataMetadata– Structured information Structured information – describes programming constructs describes programming constructs

includingincluding•Classes definitions, field and method Classes definitions, field and method

definitions, parameter lists, return types, definitions, parameter lists, return types, etc.etc.

ILDasm.exeILDasm.exe• Use ILDasm.exe to Use ILDasm.exe to

view IL for view IL for managed managed executableexecutable

Just-in-Time CompilingJust-in-Time Compiling

• All managed code runs in native All managed code runs in native machine languagemachine language

• All managed code is made up of IL and All managed code is made up of IL and metadatametadata

• The CLR JIT-compiles the IL and The CLR JIT-compiles the IL and metadatametadata– At execution timeAt execution time– Executed directly by CPUExecuted directly by CPU

• Allows for the best of both worldsAllows for the best of both worlds– Code management featuresCode management features– Performance of full-speed executionPerformance of full-speed execution

Running Process’ Memory

SomeSources.exe

IL

Metadata

JIT Compiler

10010100 10110000 10000000 1011101011011011 11010111 11000010 01110110

Native Machine Language

The CPU executes the JIT-compiled machine code directly

At execution time the IL and Metadata are JIT compiled

Executing a Managed Executing a Managed ApplicationApplication

Automatic Memory Automatic Memory ManagementManagement

• The CLR manages memory for managed The CLR manages memory for managed codecode– All allocations of objects and buffers made All allocations of objects and buffers made

from a from a Managed HeapManaged Heap– Unused objects and buffers are cleaned up Unused objects and buffers are cleaned up

automatically through automatically through Garbage CollectionGarbage Collection

• Some common software bugs are not Some common software bugs are not possible with managed codepossible with managed code– Leaked memory or objectsLeaked memory or objects– References to freed or non-existent objectsReferences to freed or non-existent objects– Reading of uninitialized variablesReading of uninitialized variables

• Impossible to gain access to memory Impossible to gain access to memory with object referencewith object reference

The Managed HeapThe Managed Heap

• Reduces bugsReduces bugs• Makes code access security possibleMakes code access security possible

– No direct memory accessNo direct memory access

• Makes type safety possibleMakes type safety possible– Incompatible type coercion not possibleIncompatible type coercion not possible

• PerformancePerformance– Newing up objects is much faster than Newing up objects is much faster than

malloc()malloc()– Garbage collection can be a perf-hitGarbage collection can be a perf-hit

Garbage CollectionGarbage Collection• Garbage collection occurs when a “new” Garbage collection occurs when a “new”

operation fails due to lack of memoryoperation fails due to lack of memory• All managed threads are stoppedAll managed threads are stopped• Collection starts with roots, and Collection starts with roots, and

recursively finds referenced objectsrecursively finds referenced objects– Roots == globals, locals, cpu registersRoots == globals, locals, cpu registers

• Referenced objects are moved down in the Referenced objects are moved down in the managed heap, managed heap, – Making space available at the endMaking space available at the end– Removing unreachable objectsRemoving unreachable objects– No FragmentationNo Fragmentation

• References fixed-up and managed threads References fixed-up and managed threads restartedrestarted

A

B

E

DC

The Managed Heap

= Object or buffer in memory

class MyClass{ void Method(){ Variable v1; Variable v2;

do{ . . .

Objects A and D will be cleaned up because neither is directly or indirectly referenced

by code

Garbage CollectionGarbage Collection

CLR SummaryCLR Summary• The platform for managed codeThe platform for managed code

– C#, VB.NET, etc.C#, VB.NET, etc.

• FeaturesFeatures– Memory management, security Memory management, security

management, reflection, type safetymanagement, reflection, type safety– IL, metadata, JIT compilationIL, metadata, JIT compilation– Assemblies, managed modulesAssemblies, managed modules

• Understanding the platform will Understanding the platform will make coding .NET code easiermake coding .NET code easier– C#, VB.NET, etc.C#, VB.NET, etc.