Tải bản đầy đủ (.pdf) (496 trang)

Wordware publishing memory management algorithms and implementation in c and c++

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (2.76 MB, 496 trang )

MemoryManagement:Algorithmsand
ImplementationinC/C++
ISBN:1556223471
byBillBlunden
WordwarePublishing©2003(360pages)
Thisbookpresentsseveralconcrete
implementationsofgarbagecollectionand
explicitmemorymanagementalgorithms.
TableofContents
MemoryManagement—Algorithmsand
ImplementationinC/C++
Introduction
MemoryManagement
Chapter1 Mechanisms
MemoryManagement
Chapter2 Policies
Chapter3 - High-LevelServices
ManualMemory
Chapter4 Management
AutomaticMemory
Chapter5 Management
Chapter6 - MiscellaneousTopics
Index
ListofFigures
ListofTables
ListofCaseStudies
ListofSidebars


MemoryManagement—Algorithmsand
ImplementationinC/C++


byBillBlunden
WordwarePublishing,Inc.
LibraryofCongressCataloging-in-PublicationData
Blunden,Bill,1969Memorymanagement:algorithmsandimplementationinC/C++/by
BillBlunden.
p.cm.
Includesbibliographicalreferencesandindex.
ISBN1-55622-347-1
1.Memorymanagement(Computerscience)2.Computeralgorithms.3.
C(Computerprogramlanguage)4.C++(Computerprogramlanguage)I.
Title.
QA76.9.M45.B5582002
005.4'35--dc212002012447
CIP
Copyright©2003,WordwarePublishing,Inc.
AllRightsReserved
2320LosRiosBoulevard
Plano,Texas75074
Nopartofthisbookmaybereproducedinanyformorbyanymeans
withoutpermissioninwritingfromWordwarePublishing,Inc.
PrintedintheUnitedStatesofAmerica
ISBN1-55622-347-1


10987654321
0208
Productnamesmentionedareusedforidentificationpurposesonlyand
maybetrademarksoftheirrespectivecompanies.
Allinquiriesforvolumepurchasesofthisbookshouldbeaddressedto
WordwarePublishing,Inc.,attheaboveaddress.Telephoneinquiries

maybemadebycalling:
(972)423-0090
ThisbookisdedicatedtoRob,Julie,andTheo.
AndalsotoDavidM.Lee
"Icametolearnphysics,andIgotJimmyStewart"
Acknowledgments
Publishingabookisanextendedprocessthatinvolvesanumberof
people.Writingthefinalmanuscriptisjustasmallpartofthebigpicture.
Thissectionisdedicatedtoallthepeoplewhodirectly,andindirectly,lent
metheirhelp.
Firstandforemost,IwouldliketothankJimHillofWordwarePublishing
forgivingmetheopportunitytowriteabookandbelievinginme.Iwould
alsoliketoextendthankstoWesBeckwithandBethKohler.Wes,in
additiontoofferingconstantencouragement,doesagreatjobofputting
upwithmye-mailsandhandlingthevariouspackagesthatIsend.Beth
Kohler,whoperformedtheincredibletaskofreadingmyfirstbookfor
Wordwareinamatterofdays,hasalsobeeninvaluable.
IfirstspokewithBarryBreybackinthemid-1990swhenIbecame
interestedinprotectedmodeprogramming.Hehasalwaystakenthetime
toanswermyquestionsandofferhisinsight.Barrywrotethefirstbookon
theIntelchipsetbackin1984.Sincethen,hehaswrittenwellover20
books.HiscurrenttextbookonIntel'sIA32processorsisinitssixth
edition.ThisiswhyIknewIhadtoaskBarrytobethetechnicaleditorfor


thisbook.Thanks,Barry.
"Look,ourmiddlewareevenrunsonthatlittleWindowsNTpieceof
crap."
—GeorgeMatkovitz
"Hey,whowasthe%&^$sonofa&*$#whowrotethisoptimized

loadof...oh,itwasme."
—MikeAdler
MikeAdlerandGeorgeMatkovitzaretwooldfogeyswhoworkedat
ControlDatabackwhenSeymourCraykickedthetaroutofIBM.George
helpedtoimplementtheworld'sfirstmessage-passingoperatingsystem
atControlData.Mikealsoworkedonanumberofgroundbreaking
systemsoftwareprojects.ImetthesetwocodgerswhileperformingR&D
foranERPvendorintheMidwest.Ihadn'tnoticedhowmuchthese
engineershadinfluencedmeuntilIleftMinnesotaforCalifornia.Itwas
almostasthoughIhadlearnedthroughosmosis.Alotofmycore
understandingofsoftwareandthecomputerindustryingeneralisbased
onthebitsofhard-wonadviceandlorethatthesegentlemenpassedon
tome.IdistinctlyrememberwalkingintoMike'sofficeandaskinghim,
"HeyMike,howdoyoubuildanoperatingsystem?"
IwouldalsoliketothankFrankMerat,aseniorprofessoratCase
WesternReserveUniversity.Frankhasconsistentlyshowninterestinmy
workandhasofferedhissupportwheneverhecould.Thereisnobetter
provinggroundforabookthananestablishedresearchuniversity.
Finally,IwouldliketothankSonicWALL,Inc.forlayingmeoffandgiving
metheopportunitytositaroundandthink.ThedaysIspenthuddledwith
mycomputerswereveryproductive.
AuthorInformation
BillBlundenhasbeenobsessedwithsystemssoftwaresincehisfirst
exposuretotheDOSdebugutilityin1983.Hissingle-mindedpursuitto
discoverwhatactuallygoesonunderthehoodledhimtoprogramthe


8259interruptcontrollerandbecomeanhonorablememberofthetriplefaultclub.AfterobtainingaBAinmathematicalphysicsandanMSin
operationsresearch,Billwasunleashedupontheworkplace.Itwasatan
insurancecompanyinthebeautifulcityofCleveland,plyinghisskillsas

anactuary,thatBillgotintohisfirstfistfightwithacrankyIBM
mainframe.Bloodybutnotbeaten,Billdecidedthatgrokingsoftwarebeat
crunchingnumbers.ThisledhimtoamajorERPplayerinthemidwest,
wherehedevelopedCASEtoolsinJava,wrestledwithCOBOL
middleware,andwasassailedbyvariousControlDataveterans.Having
aquad-processormachinewith2GBofRAMathisdisposal,Billwas
hardpressedtofindanysortofreasontoabandonhisivorytower.
Nevertheless,thebirthofhisnephewforcedhimtomakeapilgrimage
outwesttoSiliconValley.Currentlyonthepeninsula,Billsurvivesrolling
powerblackoutsandearthquakes,andisslowlyrecoveringfromhisinitial
boutwithCOBOL.


Introduction
"Paynoattentiontothemanbehindthecurtain."
—TheWizardofOz
Thereareamultitudeofacademiccomputersciencetextsthatdiscuss
memorymanagement.Theytypicallydevoteachapterorlesstothe
subjectandthenmoveon.Rarelyareconcrete,machine-leveldetails
provided,andactualsourcecodeisevenscarcer.Whentheauthoris
donewithhiswhirlwindtour,thereadertendstohaveaverylimitedidea
aboutwhatishappeningbehindthecurtain.Thisisnosurprise,given
thatthenatureofthediscussionisrampantlyambiguous.Imaginetrying
toappreciateBeethovenbyhavingsomeonereadthesheetmusictoyou
orexperiencetheMonaLisabyreadingadescriptioninaguidebook.
Thisbookisdifferent.Verydifferent.
Inthisbook,Iamgoingtopullthecurtainbackandletyouseethelittle
manoperatingtheswitchesandpulleys.Youmaybeexcitedbywhatyou
see,oryoumayfeelsorrythatyoudecidedtolook.ButasEnricoFermi
wouldagree,knowledgeisalwaysbetterthanignorance.

Thisbookprovidesanin-depthlookatmemorysubsystemsandoffers
extensivesourcecodeexamples.IncaseswhereIdonothaveaccessto
sourcecode(i.e.,Windows),Iofferadviceonhowtogatherforensic
evidence,whichwillnurtureinsight.Whilesomebooksonlygivereaders
apeakunderthehood,thisbookwillgivereadersapowerdrillandallow
themtoripoutthetransmission.Theideabehindthisistoallowreaders
tostepintothegarageandgettheirhandsdirty.
Myownexperiencewithmemorymanagersbeganbackinthelate1980s
whenBorland'sniftyTurboC1.0compilerwasreleased.Thiswasmy
firsttasteoftheClanguage.Icanrememberusingadisassemblerto
reverseengineerlibrarycodeinanattempttoseehowthemalloc()
andfree()standardlibraryfunctionsoperated.Idon'tknowhowmany
schoolnightsIspentstaringatan80x25monochromescreen,
decipheringhexdumps.Itwastoughgoingandnothorriblyrewarding


(butIwascurious,andIcouldn'thelpmyself).Fortunately,Ihavedone
mostofthedirtyworkforyou.Youwillconvenientlybeabletosidestep
allofthehurdlesandtediousmanuallaborthatconfrontedme.
Ifyouwerelikemeandenjoyedtakingyourtoysapartwhenyouwerea
childtoseehowtheyworked,thenthisisthebookforyou.Solayyour
computeronatarpaulin,breakoutyourcompilers,andgrabanoilrag.
We'regoingtotakeapartmemorymanagementsubsystemsandput
thembacktogether.Letthedustflywhereitmay!


HistoricalSetting
Inthelate1930s,agroupofscholarsarrivedatBletchleyParkinan
attempttobreaktheNazis'famousEnigmacipher.Thisgroupof
codebreakersincludedanumberofnotablethinkers,likeTommyFlowers

andAlanTuring.AsaresultoftheefforttocrackEnigma,thefirst
electroniccomputerwasconstructedin1943.ItwasnamedColossus
andusedthermionicvalves(knowntodayasvacuumtubes)forstoring
data.Othervacuumtubecomputersfollowed.Forexample,ENIAC
(electronicnumericalintegratorandcomputer)wasbuiltbytheU.S.Army
in1945tocomputeballisticfiringtables.
Note Sciencefictionaficionadosmightenjoyamoviecalled
Colossus:TheForbinProject.Itwasmadein1969andcenters
aroundColossus,asupercomputerdesignedbyascientist
namedCharlesForbin.Forbinconvincesthemilitarythatthey
shouldgivecontroloftheU.S.nucleararsenaltoColossusin
ordertoeliminatethepotentialofhumanerroraccidentally
startingWorldWarIII.ThemovieissimilarinspirittoStanley
Kubrick's2001:ASpaceOdyssey,butwithoutthehappy
ending:Robotisbuilt,robotbecomessentient,robotruns
amok.IwastoldthateveryonewhohaseverworkedatControl
Datahasseenthismovie.
Thenextearth-shakingdevelopmentarrivedin1949whenferrite(iron)
corememorywasinvented.Eachbitofmemorywasmadeofasmall,
circularironmagnet.Thevalueofthebitswitchedfrom"1"to"0"byusing
electricalwirestomagnetizethecircularloopsinoneoftwopossible
directions.ThefirstcomputertoutilizeferritecorememorywasIBM's
705,whichwasputintoproductionin1955.Backinthosedays,8KBof
memorywasconsideredahugepieceofrealestate.
Everythingchangedoncetransistorsbecamethestandardwaytostore
bits.Thetransistorwaspresentedtotheworldin1948whenBellLabs
decidedtogopublicwithitsnewdevice.In1954,BellLabsconstructed
thefirsttransistor-basedcomputer.ItwasnamedTRADIC



(TRAnsistorizedDIgitalComputer).TRADICwasmuchsmallerandmore
efficientthanvacuumtubecomputers.Forexample,ENIACrequired
1,000squarefeetandcausedpoweroutagesinPhiladelphiawhenitwas
turnedon.TRADIC,ontheotherhand,wasroughlythreecubicfeetin
sizeandranon100wattsofelectricity.
Note Beforeelectroniccomputersbecameafeasiblealternative,
heavymathematicalcomputationreliedonhumancomputers.
Largegroupsofpeoplewouldbeassembledtocarryout
massivenumericalalgorithms.Eachpersonwoulddoapartof
acomputationandpassitontosomeoneelse.Thisaccounts
fortheprevalanceoflogarithmtablesinmathematical
referencesliketheonepublishedbytheChemicalRubber
Company(CRC).Sliderulesandmathtableswerestandard
farebeforetheriseofthedigitalcalculator.
ASIDE
"After45minutesorso,we'llseethattheresultsareobvious."
—DavidM.Lee
IhaveheardNobellaureatesinphysics,likeDaveLee,complainthat
studentswhorelytooheavilyoncalculatorslosetheirmathematical
intuition.Toanextent,Daveiscorrect.Beforethedawnofcalculators,
errorsweremorecommon,anddevelopingafeelfornumeric
techniqueswasausefulwaytohelpcatcherrorswhentheyoccurred.
DuringtheLosAlamosproject,ascientistnamedDickFeynmanrana
massivehumancomputer.Heoncementionedthattheperformance
andaccuracyofhisgroup'scomputationswereoftenmoreafunction
ofhisabilitytomotivatepeople.Hewouldsometimesassemble
peopleintoteamsandhavethemcompeteagainsteachother.Not
onlywasthisagoodideafromthestandpointofmakingthingsmore
interesting,butitwasalsoaneffectivetechniqueforcatching
discrepancies.



In1958,thefirstintegratedcircuitwasinvented.Theinventorwasa
fellownamedJackKilby,whowashangingoutinthebasementofTexas
Instrumentsonesummerwhileeveryoneelsewasonvacation.Alittle
overadecadelater,in1969,Intelcameoutwitha1kilobitmemorychip.
Afterthat,thingsreallytookoff.By1999,IwasworkingonaWindowsNT
4.0workstation(servicepack3)thathad2GBofSDRAMmemory.
Thegeneraltrendyoushouldbeabletogleanfromtheprevious
discussionisthatmemorycomponentshavesolvedperformance
requirementsbygettingsmaller,faster,andcheaper.Thehardware
peoplehavebeenabletohavetheircakeandeatittoo.However,the
lawsofphysicsplacealimitonhowsmallandhowfastwecanactually
makeelectroniccomponents.Eventually,natureitselfwillstandinthe
wayofadvancement.Heisenberg'sUncertaintyPrinciple,shownbelow,
iswhatpreventsusfrombuildinginfinitelysmallcomponents.
ΔxΔp≥(h/4π)
Forthosewhoaremath-phobic,IwilluseHeinsenberg'sownwordsto
describewhatthisequationmeans:
"Themorepreciselythepositionisdetermined,thelesspreciselythe
momentumisknowninthisinstant,andviceversa."
Inotherwords,ifyouknowexactlywhereaparticleis,thenyouwillnot
beabletocontainitbecauseitsmomentumwillbehuge.Thinkofthis
liketryingtocatchatomatoseed.Everytimeyoutrytosqueezedown
andcatchit,theseedshootsoutofyourhandsandfliesacrossthe
dinnertableintoUncleDon'sface.
Einstein'sGeneralTheoryofRelativityiswhatkeepsusfrombuilding
infinitelyfastcomponents.Withtheexceptionofblackholes,thespeed
limitinthisuniverseis3x108meterspersecond.Eventually,thesetwo
physicallimitsaregoingtocreepuponus.

Whenthishappens,thehardwareindustrywillhavetoeithermakelarger
chips(inanefforttofitmoretransistorsinagivenarea)orusemore
efficientalgorithmssothattheycanmakebetteruseofexistingspace.


Myguessisthatrelyingonbetteralgorithmswillbethecheaperoption.
Thisisparticularlytruewithregardtomemorymanagement.Memory
manipulationissofrequentandcrucialtoperformancethatdesigning
bettermemorymanagementsubsystemswilltakecenterstageinthe
future.Thiswillmakethetimespentreadingthisbookagood
investment.


Chapter1:MemoryManagementMechanisms


Overview
"Everyonehasaphotographicmemory.Somepeoplejustdon'thave
film."
—MelBrooks
Note Inthetextofthisbook,italicsareusedtodefineoremphasize
aterm.TheCourierfontisusedtodenotecode,memory
addresses,input/output,andfilenames.Formoreinformation,
seethesectiontitled"TypographicalConventions"inthe
Introduction.


Chapter2:MemoryManagementPolicies



Overview
"IfIcouldrememberthenamesofalltheseparticles,I'dbea
botanist."
—EnricoFermi
Inthepreviouschapter,Idiscussedthebasicmechanismsthat
processorsprovidetoallowmemoryregionstoberead,modified,
isolated,andsimulated.Nowyouarereadytoexaminethewaysinwhich
operatingsystemsconstructpoliciesthatmakeuseofthese
mechanisms.Theprocessorpresentsthemeanstodothingswith
memorythroughaseriesofdedicateddatastructures,system
instructions,andspecialregisters.Itoffersasetofprimitivesthatcanbe
combinedtoformanumberofdifferentprotocols.Itisentirelyuptothe
operatingsystemtodecidehowtousetheprocessor'sfundamental
constructs,oreventousethematall.
Therearedozensofoperatingsystemsinproduction.Eachonehasits
owndesigngoalsanditsownwayofdecidinghowtousememory
resources.InthischapterIwilltakeanin-depthlookatthememory
subsystemsofseveralkernels,rangingfromthesimpletothe
sophisticated.IwillscrutinizesourcecodewhenIcanandhopefullygive
youabetterfeelforwhatisgoingoninsidetheLeMarchandcube.
Inthischapter,Iamgoingtograduallyrampupthelevelofcomplexity.I
willstartwithDOS,whichispossiblythemoststraightforwardandsimple
operatingsystemthatrunsonaPC.DOSisreallynothingmorethana
thinlayerofcodebetweenyouandthehardware.Next,Iwillkickthe
difficultyupanotchwithMMURTL.MMURTL,unlikeDOS,isa32-bit
operatingsystemthatrunsinprotectedmode.Finally,thischapterwill
culminatewithadiscussionoftwoproduction-qualitysystems:Linuxand
Windows.
Afterhavinglookedatallfouroperatingsystems,IthinkthatWindowsis
themostcomplicatedsystem.Anyonewhodisagreeswithmeshould

compareimplementingaloadablekernelmoduleforLinuxwithwritinga


kernelmodePnPdriverforWindows.Therearepeoplewhomakea
livingoffoftellingpeoplehowtowriteWindowskernelmodedrivers.
Don'tgetmewrong,thedocumentationforwritingkernelmodedriversis
accessibleandcopious;itisjustthattheprocessissoinvolved.After
literallywadingthroughWindows,Igainedanappreciationforthe
relativelystraightforwardnatureoftheLinuxkernel.


Chapter3:High-LevelServices
"MyproblemisthatIhavebeenpersecutedbyaninteger."
—GeorgeA.Miller


Viewfrom10,000Feet
Acomputer'smemorymanagementsubsystemcanbelikenedtoa
house.Thefoundationandplumbingareprovidedbythehardware.Itis
alwaysthere,doingitsjobbehindthescenes;youjusttakeitforgranted
untilsomethingbreaks.Theframeofthehouseissuppliedbythe
operatingsystem.Theoperatingsystemisbuiltuponthefoundationand
givesthehouseitsformanddefinesitsfunctionality.Awell-builtframe
canmakethedifferencebetweenashackandamansion.
Itwouldbepossibletostopwiththeoperatingsystem'smemory
managementfacilities.However,thiswouldbelikeahousethathasno
furnitureorappliances.Itwouldbeaprettyaustereplacetolivein.You
wouldhavetosleeponthefloorandusethebathroomoutside.User
spacelibrariesandtoolsarewhatfurnishtheoperatingsystemwith
amenitiesthatmakeiteasierforapplicationstouseandexecutewithin

memory.High-levelserviceslikethesearewhataddutilitytothehouse
andgiveitresalevalue(seeFigure3.1onthefollowingpage).

Figure3.1
Therearetwowaysthatuserapplicationscanallocatememory:
compiler-basedallocationandheapallocation.
Wewillspendthischapteranalyzingbothofthesetechniques.
Thefirstapproachissupported,tovariousdegrees,bythedevelopment
environmentthatisbeingused.Notallcompilers,andthelanguagesthey
translate,areequal.Youwillseeagraphicdemonstrationofthislateron
inthechapter.


Thesecondapproachisnormallyimplementedthroughlibrarycalls(i.e.,
likemalloc()andfree())orbyaresidentvirtualmachine.Using
thistechniquetoimplementmemorymanagementprovidesawayfor
storageallocationfacilitiestobedecoupledfromthedevelopmenttools.
Forexample,thereareseveraldifferentimplementationsofmalloc()
thatcanbeusedwiththegcccompiler.Someengineersevenspecialize
inoptimizingmalloc()andoffertheirownhigh-performance
malloc.tar.gzpackagesasadrop-inreplacementforthestandard
implementation.
Inordertohelpillustratethesetwoapproaches,Iwilllookatseveral
developmentenvironments.Thiswillgiveyoutheopportunitytoseehow
differenttoolsandlibrariesprovidehigh-levelservicestouser
applications.Wewillbegiventheluxuryofforgettingaboutthehardware
detailsandbeabletolookatmemoryfromamoreabstractvantage
point.Iwillbeginbylookingatrelativelysimplelanguages,likeCOBOL,
andthenmoveontomoresophisticatedlanguages,likeCandJava.
Note Somepeopleprefertoclassifymemoryallocationtechniquesin

termsofwhethertheyarestaticordynamic.Staticmemoryis
memorythatisreservedfromthemomentaprogramstarts
untiltheprogramexits.Staticmemorystoragecannotchange
size.Itsuseandpositionrelativetootherapplication
componentsistypicallydeterminedwhenthesourcecodefor
theapplicationiscompiled.
Dynamicmemoryismemorythatisrequestedandmanaged
whiletheprogramisrunning.Dynamicmemoryparameters
cannotbespecifiedwhenaprogramiscompiledbecausethe
sizeandlifespanfactorsarenotknownuntilruntime.
Whiledynamicmemorymayallowgreaterflexibility,usingstatic
memoryallowsanapplicationtoexecutefasterbecauseit
doesn'thavetoperformanyextraneousbookkeepingat
runtime.Inaproductionenvironmentthatsupportsalarge
numberofapplications,usingstaticmemoryisalsosometimes
preferablebecauseitallowsthesystemadministratorsto
implementaformofloadbalancing.Ifyouknowthatacertain


applicationhasafootprintinmemoryofexactly2MB,thenyou
knowhowmanyserversyouwillneedtoprovide300instances
oftheapplication.
Ithinkthatthestatic-versus-dynamicschememakesitmore
complicatedtocategorizehybridmemoryconstructslikethe
stack.ThisiswhyIamstickingtoacompiler-versus-heap
taxonomy.


Chapter4:ManualMemoryManagement
Managingmemoryintheheapisdefinedbytherequirementthat

servicesbeprovidedtoallocateanddeallocatearbitrarysizeblocksof
memoryinanarbitraryorder.Inotherwords,theheapisafree-for-all
zone,andtheheapmanagerhastobeflexibleenoughtodealwitha
numberofpossiblerequests.Therearetwowaystomanagetheheap:
manualandautomaticmemorymanagement.Inthischapter,Iwilltake
anin-depthlookatmanualmemorymanagementandhowitis
implementedinpractice.


Replacementsformalloc()andfree()
Manualmemorymanagementdictatesthattheengineerwritinga
programmustkeeptrackofthememoryallocated.Thisforcesallofthe
bookkeepingtobeperformedwhentheprogramisbeingdesigned
insteadofwhiletheprogramisrunning.Thiscanbenefitexecutionspeed
becausetherelatedbookkeepinginstructionsarenotplacedinthe
applicationitself.However,ifaprogrammermakesanaccountingerror,
theycouldbefacedwithamemoryleakoradanglingpointer.
Nevertheless,properlyimplementedmanualmemorymanagementis
lighterandfasterthanthealternatives.Iprovidedevidenceofthisinthe
previouschapter.
InANSIC,manualmemorymanagementisprovidedbythemalloc()
andfree()standardlibrarycalls.Therearetwootherstandardlibrary
functions(calloc()andrealloc()),butaswesawinChapter3,they
resolvetocallstomalloc()andfree().
Ithoughtthatthebestwaytoillustratehowmanualmemorymanagement
facilitiesareconstructedwouldbetoofferseveraldifferent
implementationsofmalloc()andfree().Tousethesealternative
implementations,allyouwillneedtodoisincludetheappropriatesource
fileandthencallnewMalloc()andnewFree()insteadofmalloc()
andfree().Forexample:

#include<mallocV1.cpp>
voidmain()
{
char*cptr;
initMemMgr();
cptr=newMalloc(10);
if(cptr==NULL){printf("allocationfailed!\n");}
newFree(cptr);


closeMemMgr();
return;
}
Theremainderofthischapterwillbedevotedtodescribingthreedifferent
approaches.Ineachcase,Iwillpresenttherequisitebackgroundtheory,
offeraconcreteimplementation,provideatestdriver,andlookat
associatedtrade-offs.Alongtheway,Iwillalsodiscussperformance
measuringtechniquesandissuesrelatedtoprogramsimulation.


Chapter5:AutomaticMemoryManagement
Automaticmemorymanagerskeeptrackofthememorythatisallocated
fromtheheapsothattheprogrammerisabsolvedoftheresponsibility.
Thismakeslifeeasierfortheprogrammer.Infact,notonlydoesitmake
theprogrammer'sjobeasier,butitalsoeliminatesothernastyproblems,
likememoryleaksanddanglingpointers.Thedownsideisthatautomatic
memorymanagersaremuchmoredifficulttobuildbecausetheymust
incorporatealltheextrabookkeepingfunctionality.
Note Automaticmemorymanagersareoftenreferredtoasgarbage
collectors.Thisisbecauseblocksofmemoryintheheapthat

wereallocatedbyaprogram,butwhicharenolonger
referencedbytheprogram,areknownasgarbage.Itisthe
responsibilityofagarbagecollectortomonitortheheapand
freegarbagesothatitcanberecycledforotherallocation
requests.


GarbageCollectionTaxonomy
Takingoutthetrashisadancewithtwosteps:
1. Identifyinggarbageintheheap
2. Recyclinggarbageonceitisfound
Thedifferentgarbagecollectionalgorithmsaredistinguishedintermsof
themechanismsthattheyusetoimplementthesetwosteps.For
example,garbagecanbeidentifiedbyreferencecountingorbytracing.
Mostgarbagecollectorscanbecategorizedintooneofthesetwotypes.
Referencecountingcollectorsidentifygarbagebymaintainingarunning
tallyofthenumberofpointersthatreferenceeachblockofallocated
memory.Whenthenumberofreferencestoaparticularblockofmemory
reacheszero,thememoryisviewedasgarbageandreclaimed.There
areanumberoftypesofreferencecountingalgorithms,eachone
implementingitsownvariationofthecountingmechanism(i.e.,simple
referencecounting,deferredreferencecounting,1-bitreferencecounting,
etc.).
Tracinggarbagecollectorstraversetheapplicationrun-timeenvironment
(i.e.,registers,stack,heap,datasection)insearchofpointerstomemory
intheheap.Thinkoftracingcollectorsaspointerhunter-gatherers.Ifa
pointerisfoundsomewhereintherun-timeenvironment,theheap
memorythatispointedtoisassumedtobe"alive"andisnotrecycled.
Otherwise,theallocatedmemoryisreclaimed.Thereareseveral
subspeciesoftracinggarbagecollectors,includingmark-sweep,markcompact,andcopyinggarbagecollectors.

Anoutlineofdifferentautomaticmemorymanagementapproachesis
providedinFigure5.1.


×