Managed Code: MSIL, Metadata and JIT Compilation; Automatic Memory Management

VB.NET एक assembly के अंदर MSIL plus metadata में compile होता है; JIT compiler run time पर MSIL को native code में बदलता है, और garbage collector memory manage करता है: वह पूरी supervised life managed code है।

10 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

Till असल में क्या Execute करता है

ShopKeeper finished है (आज के लिए imagine कीजिए)। आप Build press करते हैं, ShopKeeper.exe को Mehta Stores के till पर copy करते हैं, और यह run होता है।

Small puzzle: till का CPU raw machine instructions execute करता है। आपका .exe आपके laptop पर बना था, उस CPU के बारे में कुछ जाने बिना। तो file के अंदर exactly क्या है, और till पर इसे कौन translate करता है?

Answer एक 2-stage pipeline है जिस पर आप पहले से Java से trust करते हैं, Microsoft names पहने हुए, plus एक word जो इसे साथ बाँधता है: managed।

Theory

Stage 1: MSIL + Metadata में Compile करना

VB.NET compiler machine code produce NAHI करता। यह produce करता है:

  • MSIL (Microsoft Intermediate Language): CPU-neutral instructions, .NET का Java bytecode के equivalent
  • metadata: साथ में packed एक full self-description: code में हर class, method, property और reference

दोनों एक assembly में साथ travel करते हैं: .exe या .dll file। Metadata यही वजह है Visual Studio आपकी classes autocomplete कर सकता है और एक C# project आपके VB.NET code को call कर सकता है: assembly पूछने वाले किसी को भी खुद को explain करती है।

Theory

Stage 2: JIT, फिर GC Watch लेता है

Till पर, CLR assembly load करता है। जैसे हर method पहली बार call होता है, JIT (just-in-time) compiler इसकी MSIL को उस machine के CPU के लिए native code में translate करता है, और result cache करता है ताकि बाद की calls full native speed पर run हों।

और जब तक ShopKeeper run होता है, CLR का garbage collector memory watch करता है: कोई भी object जिसकी तरफ़ अब कोई reference नहीं point करता automatically reclaim हो जाता है। कोई delete नहीं, कोई free नहीं, भूली हुई cleanup से कोई memory leaks नहीं: runtime वह responsibility उठाता है।

Formula

Managed Code, Exam Definition

Managed code वह code है जो CLR की supervision के तहत execute होता है: metadata के साथ MSIL में compiled, run time पर JIT-compiled, garbage collector से handled memory, execution से पहले verified types।

वह code जो इस supervision के बिना OS पर directly run होता है (BCA104/BCA304 का classic C/C++) unmanaged code है: metal के करीब faster, पर हर crash, leak और stray pointer पूरी तरह आपका है।

Quiz

MSIL native machine code में कब convert होता है?

  1. Build time पर, जब आप Visual Studio में Build press करते हैं
  2. Run time पर, method by method, जब हर एक पहली बार call होता है
  3. कभी नहीं: CLR MSIL instructions को directly execute करता है, एक script की तरह
  4. जब assembly target machine पर copy होती है
Show the answer

Run time पर, method by method, जब हर एक पहली बार call होता है

यही exactly just-in-time का मतलब है: translation आख़िरी moment पर होता है, per method, उस machine पर जो इसे run करेगी, और native result run के बाकी हिस्से के लिए cache हो जाता है। Option A एक C compiler जो करता है वह describe करता है, और .exe को एक CPU से chain कर देता। Option C CLR को undersell करता है: यह MSIL को line by line interpret करने के बजाय compile करता है। Option D एक translation step invent करता है जो copying simply perform नहीं करती।

Think first

सीधे Machine Code में Compile क्यों नहीं?

2 stages extra work जैसे दिखते हैं। Tap करने से पहले, machine code के बजाय MSIL ship करने के 2 concrete wins ढूँढिए। (Hint: till के CPU के बारे में सोचिए, और बगल वाली C# team के बारे में।)

Show the answer

Machines के आर-पार Portability: same assembly किसी भी CLR वाले CPU पर correctly JIT-compile होती है; native output को हर processor family के लिए एक build चाहिए होता। Language Interoperability: हर .NET language same MSIL + metadata में land करती है, तो आपकी VB.NET classes C# से unchanged usable हैं: एक shared landing ground। JIT की trick add कीजिए run time पर मिलने वाले exact CPU के लिए optimise करने की, और 2-stage detour direct route जैसा लगने लगता है।

Watch out

Terms जिन्हें Students Blur करते हैं

MSIL Metadata नहीं है: MSIL instructions है; metadata code OF description है (इसके types और members)। वे assembly में साथ ship होते हैं पर अलग exam questions answer करते हैं।

JIT VB Compiler नहीं है: vbc आपके laptop पर एक बार run होता है (source से MSIL); JIT हर execution machine पर run होता है (MSIL से native)।

GC Memory Free करता है, Files या Database Connections नहीं: उन्हें अभी भी explicit closing चाहिए, जैसा Unit 5 insist करेगी।

Theory

Unit को Rule करने के लिए एक Diagram

इसे एक बार draw कीजिए और दोनों vocabularies में label कीजिए:

source (.vb / .java) → compiler (vbc / javac) → intermediate (MSIL / bytecode) + metadata → engine (CLR / JVM) → JIT → native code, नीचे GC के साथ।

वह single picture managed-code question, MSIL question, JIT question AND compare-with-Java question answer करती है, जो इनके बीच लगभग हर BCA404 paper में appear होते हैं। अगला lesson engine के अंदर zoom करता है: JIT और GC के अलावा CLR और क्या करता है।

Summary

Key takeaways

  • VB.NET एक assembly (.exe/.dll) में metadata के साथ packaged MSIL (CPU-neutral instructions) में compile होता है।
  • Metadata assembly की self-description है: types, members, references; यह IntelliSense और cross-language use को power करता है।
  • JIT compiler run time पर MSIL को native code में translate करता है, per method पहली call पर, run के लिए cached।
  • Garbage collector कोई references न वाले objects automatically reclaim करता है: automatic memory management।
  • Managed code = CLR supervision के तहत running code; classic C/C++ unmanaged है।
  • Java जैसा same architecture: vbc/javac, MSIL/bytecode, CLR/JVM, दोनों में JIT और GC।
  • Memory hook: middle तक एक बार compile कीजिए, till पर translation खत्म कीजिए।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Overview of Microsoft .NET Framework

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Managed Code: MSIL, Metadata and JIT Compilation; Automatic Memory Management · .NET Programming · Gri-Learn