managed vs unmanaged code

Upload: svinothkumar

Post on 06-Apr-2018

215 views

Category:

Documents


1 download

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.