managed vs unmanaged code
TRANSCRIPT
-
8/3/2019 Managed vs UnManaged Code
1/2
ManagedCode
Managedcodeiscodethatiswrittentotargettheservicesofthemanagedruntimeexecutionenvironment(likeCommonLanguageRuntimein.NETFramework).Themanagedcodeisalwaysexecutedbyamanagedruntimeexecutionenvironmentratherthantheoperatingsystemdirectly.Managedreferstoamethodofexchanginginformationbetweentheprogramandtheruntimeenvironment.Becausetheexecution
ofcodeisgovernedbytheruntimeenvironment,theenvironmentcanguaranteewhatthecodeisgoingtodoandprovidethenecessarysecuritychecksbeforeexecutinganypieceofcode.BecauseofthesamereasonthemanagedcodealsogetsdifferentservicesfromtheruntimeenvironmentlikeGarbageCollection,typechecking,exceptionhandling,boundschecking,etc.Thiswaymanagedcodedoesnothavetoworryaboutmemoryallocations,typesafety,etc.ApplicationswritteninJava,C#,VB.NET,etctargetaruntimeenvironmentwhichmanagestheexecutionandthecodewrittenusingthesetypesoflanguagesisknownasManagedCode.ManagedcodeisalwayscompiledintoanIntermediateLanguage(MSILincaseof.NETFramework).Thecompilerusedby.NETframeworktocompilemanagedcodecompilesitintoIntermediateLanguageandgeneratesthenecessarymetadata,symbolicinformationthatdescribesalloftheentrypointsandtheconstructsexposed
intheIntermediateLanguage(e.g.,methods,properties)andtheircharacteristics.TheCommonLanguageInfrastructure(CLI)Standarddescribeshowtheinformationistobeencoded,andprogramminglanguagesthattargettheruntimeemitthecorrectencoding.
In.NETFrameworkManagedCoderunswithinthe.NetFrameworksCLRandbenefitsfromtheservicesprovidedbytheCLR.Whenwecompilethemanagedcode,thecodegetscompiledtoanintermediatelanguage(MSIL)andanexecutableiscreated.WhenauserrunstheexecutabletheJustInTimeCompilerofCLRcompilestheintermediatelanguageintonativecodespecifictotheunderlyingarchitecture.Sincethistranslationhappensbythemanagedexecutionenvironment(CLR),themanagedexecutionenvironmentcanmakeguaranteesaboutwhatthecodeisgoingtodo,becauseitcanactuallyreasonaboutit.Itcaninserttrapsandsortofprot
ectionaround,ifit'srunninginasandboxedenvironment,itcaninsertalltheappropriategarbagecollectionhooks,exceptionhandling,typesafety,arraybounce,indexcheckingandsoforth.
Managedcodealsoprovidesplatformindependence.Asthemanagedcodeisfirstcompiledtointermediatelanguage,theCLRsJITCompilertakescareofcompilingthisintermediatelanguageintothearchitecturespecificinstructions.
UnmanagedCode
CodethatisdirectlyexecutedbytheOperatingSystemisknownasun-managedco
de.TypicallyapplicationswritteninVB6.0,C++,C,etcareallexamplesofunmanagedcode.Unmanagedcodetypicallytargetstheprocessorarchitectureandisalwaysdependentonthecomputerarchitecture.Unmanagedcodeisalwayscompiledtotargetaspecificarchitectureandwillonlyrunontheintendedplatform.Thismeansthatifyouwanttorunthesamecodeondifferentarchitecturethenyouwillhavetorecompilethecodeusingthatparticulararchitecture.Unmanagedcodeisalwayscompiledtothenativecodewhichisarchitecturespecific.WhenwecompileunmanagedcodeitgetscompiledintoabinaryX86image.Andthisimagealwaysdependsontheplatformonwhichthecodewascompiledandcannotbeexecutedontheotherplatformsthataredifferentthattheoneonwhichthecodewascompiled.Unmanagedcodedoesnotgetanyservicesfromthemanagedexecutionenvironment.
Inunmanagedcodethememoryallocation,typesafety,security,etcneedstobetakencareofbythedeveloper.Thismakesunmanagedcodepronetomemoryleakslikebufferoverrunsandpointeroverridesandsoforth.
-
8/3/2019 Managed vs UnManaged Code
2/2
Unmanagedexecutablefilesarebasicallyabinaryimage,x86code,loadedintomemory.TheprogramcountergetsputthereandthatsthelasttheOperatingSystemknows.ThereareprotectionsinplacearoundmemorymanagementandportI/Oandsoforth,butthesystemdoesntactuallyknowwhattheapplicationisdoing.