Transcript Document

Introducing the Common
Language Runtime for .NET
The Common Language Runtime
• The Common Language Runtime (CLR)
– Execution engine of The .NET Framework
– Offers many system services
– Similar to an OS
– Runs on a host OS
• The .NET Framework
– New platform by Microsoft
– Designed to target the Internet
The Purpose of the CLR…
• Safe binary execution
– Security, memory protection
– Running un-trusted code, locally
• Performance and Functionality
– Native execution (JIT Compiled)
– No Sandbox
…The Purpose of the CLR
• Bug reduction
– More code interacting, bugs affect more users
• Ease of integration
• Developer investment
– Reuse skills from project to project, even if
the projects are vastly different
Managed Code and the CLR
• The Common Language Runtime (CLR) is a
runtime engine
– Manages .NET Code (such as C# applications)
– Provides features such as memory management,
thread management, object type safety, security,
etc.
– Is a part of the .NET Framework
The CLR and Managed Code
Managed Executable
Legacy
Software
(unmanaged code)
Reusable
Managed Components
Common Language Runtime
(JIT compilation, memory management, etc.)
Windows
(or other operating oystem)
What is Managed Code
• Code that targets the CLR
– C#, VB.NET, PERL, Java, C++, Cobol, etc.
• All Managed code has all features of CLR
– Object oriented infrastructure
– Internet oriented infrastructure
– Access to the Framework Class Library (FCL)
• All Managed executables consist of
– Intermediate Language (IL) instructions
– Metadata
IL and Metadata (MSIL –
microsoft intermediate language)
• IL CPU independent machine language
– Just-in-time compiled at runtime
– Makes cross-language integration possible.
• Metadata
– Structured information
– describes programming constructs
including
• Classes definitions, field and method
definitions, parameter lists, return types, etc.
ILDasm.exe
• Use ILDasm.exe to
view IL for managed
executable
Just-in-Time Compiling
• All managed code runs in native machine
•
•
language
All managed code is made up of IL and
metadata
The CLR JIT-compiles the IL and metadata
– At execution time
– Executed directly by CPU
• Allows for the best of both worlds
– Code management features
– Performance of full-speed execution
Executing a Managed Application
SomeSources.exe
Metadata
IL
At execution time the IL and
Metadata are JIT compiled
Running Process’ Memory
JIT Compiler
Native
Machine Language
10010100 10110000 10000000 10111010
11011011 11010111 11000010 01110110
The CPU executes the JITcompiled machine code directly
Automatic Memory Management
• The CLR manages memory for managed code
– All allocations of objects and buffers made from a
Managed Heap
– Unused objects and buffers are cleaned up
automatically through Garbage Collection
• Some common software bugs are not
possible with managed code
– Leaked memory or objects
– References to freed or non-existent objects
– Reading of uninitialized variables
• Impossible to gain access to memory with
object reference
The Managed Heap
• Reduces bugs
• Makes code access security possible
– No direct memory access
• Makes type safety possible
– Incompatible type coercion not possible
• Performance
– Newing up objects is much faster than
malloc()
– Garbage collection can be a perf-hit
Garbage Collection
• Garbage collection occurs when a “new”
•
•
operation fails due to lack of memory
All managed threads are stopped
Collection starts with roots, and recursively finds
referenced objects
– Roots == globals, locals, cpu registers
• Referenced objects are moved down in the
managed heap,
– Making space available at the end
– Removing unreachable objects
– No Fragmentation
• References fixed-up and managed threads
restarted
Garbage Collection
class MyClass{
void Method(){
Variable v1;
Variable v2;
The Managed Heap
B
do{
.
.
.
Objects A and D will
be cleaned up because
neither is directly or
indirectly referenced
by code
A
E
C
= Object or buffer in memory
D
CLR Summary
• The platform for managed code
– C#, VB.NET, etc.
• Features
– Memory management, security management,
reflection, type safety
– IL, metadata, JIT compilation
– Assemblies, managed modules
• Understanding the platform will make
coding .NET code easier
– C#, VB.NET, etc.