Theory
તિલ actually શું execute કરે છે
ShopKeeper finished છે (imagine, for today). તમે Build દબાવો છો, ShopKeeper.exe ને Mehta Stores ના till પર copy કરો છો, અને તે runs કરે છે.
નાનું puzzle: till નું CPU raw machine instructions ને execute કરે છે. તમારું .exe તમારા LAPTOP પર build થયું હતું, તે CPU વિશે કંઈ જાણ્યા વગર. એટલે file ની અંદર exactly શું છે, અને કોણ તેને till પર translate કરે છે?
answer એ 2-stage pipeline છે જે તમે already Java થી trust કરો છો, Microsoft names પહેરેલા, plus એક word જે તેને tie together કરે છે: managed.
Theory
Stage 1: MSIL + metadata માં compile કરો
VB.NET compiler machine code ને produce નથી કરતું. તે produce કરે છે:
- MSIL (Microsoft Intermediate Language): CPU-neutral instructions, Java bytecode નો .NET equivalent
- metadata: full self-description packed alongside: 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 first called થાય છે, તેમ JIT (just-in-time) compiler તેના MSIL ને THAT machine ના CPU માટે native code માં translate કરે છે, અને result ને cache કરે છે એટલે later calls full native speed પર run થાય છે.
અને જ્યારે ShopKeeper runs કરે છે, CLR નો garbage collector memory ને watch કરે છે: કોઈ પણ object જેના પર કોઈ reference point નથી કરતું તે automatically reclaimed થાય છે. no delete, no free, no memory leaks ભૂલેલા cleanup થી: runtime એ responsibility ને carry કરે છે.
Formula
Managed code, exam definition
Managed code એ code છે જે CLR ની supervision નીચે execute થાય છે: MSIL માં compiled metadata સાથે, run time પર JIT-compiled, memory garbage collector દ્વારા handled, types execution પહેલાં verified.
code જે OS પર directly runs કરે છે આ supervision વગર (BCA104/BCA304 નું classic C/C++) એ unmanaged code છે: metal સુધી faster, પણ દરેક crash, leak અને stray pointer entirely yours છે.
Quiz
MSIL ને native machine code માં ક્યારે convert કરવામાં આવે છે?
- build time પર, જ્યારે તમે Visual Studio માં Build દબાવો છો
- run time પર, method by method, જ્યારે દરેક first called થાય છે
- ક્યારેય નહીં: CLR MSIL instructions ને directly execute કરે છે, script જેવું
- જ્યારે assembly ને target machine પર copy કરવામાં આવે છે
Show the answer
run time પર, method by method, જ્યારે દરેક first called થાય છે
એ exactly એ છે જે just-in-time નો અર્થ છે: translation છેલ્લી moment પર happen થાય છે, per method, તે machine પર જે તેને run કરશે, અને native result ને rest of the run માટે cache કરવામાં આવે છે. Option A describe કરે છે કે C compiler શું કરે છે, અને .exe ને one CPU સાથે chain કરતું. Option C CLR ને undersell કરે છે: તે MSIL ને line by line interpret કરવાને બદલે compile કરે છે. Option D translation step ને invent કરે છે જે copying simply perform નથી કરતું.
Think first
શા માટે straight machine code માં compile નહીં?
બે stages extra work જેવા લાગે છે. tap કરતા પહેલા, 2 concrete wins શોધો MSIL ને ship કરવાના native code ને બદલે. (Hint: till ના CPU વિશે think કરો, અને બાજુમાં C# team વિશે.)
Show the answer
machines આરપાર Portability: same assembly કોઈ પણ CPU પર CLR સાથે correctly JIT-compile થાય છે; native output ને એક build per processor family ની જરૂર પડતી. Language interoperability: દરેક .NET language same MSIL + metadata માં lands થાય છે, એટલે તમારા VB.NET classes ને C# થી unchanged usable છે: એક shared landing ground. JIT નો trick add કરો કે તે exactly CPU ને optimise કરે છે જે તે run time પર find કરે છે, અને 2-stage detour direct route જેવું look કરવાનું start કરે છે.
Watch out
Terms જે students blur together કરે છે
MSIL metadata નથી: MSIL એ instructions છે; metadata એ code નું description છે (તેના types અને members). તેઓ assembly માં સાથે ship થાય છે પણ different exam questions ને answer કરે છે.
JIT VB compiler નથી: vbc તમારા laptop પર once run થાય છે (source થી MSIL); JIT દરેક execution machine પર run થાય છે (MSIL થી native).
GC memory ને free કરે છે, files અથવા database connections નહીં: તેઓ હજુ explicit closing ને need કરે છે, જેમ Unit 5 insist કરશે.
Theory
એક diagram જે unit ને rule કરે છે
એકવાર 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 ને answer કરે છે, MSIL question ને, JIT question ને AND compare-with-Java question ને, જે between them almost દરેક BCA404 paper માં appear થાય છે. next lesson engine itself માં zoom કરે છે: CLR શું else કરે છે JIT અને GC સિવાય.
Summary
Key takeaways
- VB.NET MSIL (CPU-neutral instructions) માં compile થાય છે જે metadata સાથે assembly (.exe/.dll) માં packaged છે.
- Metadata એ assembly નું self-description છે: types, members, references; તે IntelliSense અને cross-language use ને power કરે છે.
- JIT compiler MSIL ને native code માં run time પર translate કરે છે, per method on first call, run માટે cached.
- Garbage collector automatically objects ને reclaim કરે છે જેના કોઈ references નથી: automatic memory management.
- Managed code = code જે CLR supervision નીચે run થાય છે; classic C/C++ unmanaged છે.
- Java સાથે same architecture: vbc/javac, MSIL/bytecode, CLR/JVM, JIT અને GC બંને માં.
- Memory hook: middle સુધી once compile કરો, till પર translation ને finish કરો.