Transcript

SustainableMemoryUseinJavaProgramsAlloca&on&(Implicit)Dealloca&oninJava

OriginallygiveninComp215

COMP412FALL2017

Copyright2017,KeithD.Cooper,allrightsreserved.StudentsenrolledinComp412atRiceUniversityhaveexplicitpermissiontomakecopiesofthesematerialsfortheirpersonaluse.FacultyfromeducaKonalinsKtuKonsmayusethesematerialsfornonprofiteducaKonalpurposes,providedthiscopyrightnoKceispreserved.

Preliminaries

Today’slectureFocusfortodayisontheJavamemorymodel,allocaKon,&recycling→  Howitworks→  Howitaffectsyourcode’srunKme

→  Howyoushouldprogramdefensivelynowthatyouknow

COMP412,SupplementalMaterial 1

Wheredoobjectslive?

COMP140&COMP215(sofar)•  Wehaveencouragedyoutoignoretheissueofwhereobjects,variables,andmethodslive

•  TheimplementaKon(PythonorJava)takescareofthesedetails

•  Fundamentally,abstracKonisagoodthing—rightuptothepointwhereitcausesproblems

•  AtsomepointinyourJavacareer,performancewillma[er–  COMP215exerciseswhereyourcodeisKmed,orCOMP412

–  Atthatpoint,youneedtopaya[enKontodetails

•  Today’slectureisaboutdetailsCOMP412,SupplementalMaterial 2

JavaWorld

Fred Kate

JuliaSystem.out.

println()

Wheredoobjectslive?

TheJavaSystemmapsJavaWorldontoProcessorResources•  Processorhasfiniteresources•  Javasuggeststhatyouhave“enough”resources

•  Mapping“enough”onto“what’sthere”isthejoboftheJavacompilerandrunKme(JVM)

Knowinghowthatmappingworkscanhelpyouunderstandthebehaviorofyourprograms,andsuggestwaystoimprovetheprogram’sbehavior.

COMP412,SupplementalMaterial 3

JavaWorld

Fred Kate

JuliaSystem.out.

println()

0 1 2 k

RAM

ProcessorCore

ProcessorCore

ProcessorCore ���

Fundamentals

Intheexample,whatneedsstorage?•  Thetwoclasses(Point&C)•  Point’slocalmembers(x,y,&draw)•  C’slocalmembers(s,t,&m)•  m’slocalvariables(a,b,&p)

COMP412,SupplementalMaterial 4

ClassPoint{ publicintx,y; publicvoiddraw();

}ClassC{ ints,t; publicvoidm() { inta,b;

Pointp=newPoint();a=…; b=…; p.draw(); }

}

Aclassicexample

Fundamentals

Intheexample,whatneedsstorage?•  Thetwoclasses(Point&C)•  Point’slocalmembers(x,y,&draw)•  C’slocalmembers(s,t,&m)•  m’slocalvariables(a,b,&p)MemoryintheJavarunKmeisdivided,broadlyspeaking,intoaHeapandacollecKonofStacks•  Oneheapperprogram(large)•  Onestackperthread(smaller)

COMP412,SupplementalMaterial 5

ClassPoint{ publicintx,y; publicvoiddraw();

}ClassC{ ints,t; publicvoidm() { inta,b;

Pointp=newPoint();a=…; b=…; p.draw(); }

}

Aclassicexample

Heap

STAC

K 0

STAC

K 1

STAC

K 2

STAC

K n…

Point C

“new”point

“Helloworld!”

StringPool

p:a:b:

JVMMemoryLayout

Conceptually,Javamemoryislaidoutalongtheselines

•  Whenrunningcodecreatesavariable,itgoesintothethread’sstack•  Whenrunningcodecreatesaclassoranobject(e.g.,withanew),itgoesintotheheap•  Codelivesofftothelef (mightconsideritpartoftheheap)

COMP412,SupplementalMaterial 6

So,canaprogramrunoutofheapspace? (toomanynews)

Whathappens?⇒TherunKmesystemtriestorecyclespaceontheheap

Heap

Stacks

Growthsp

ace

forstacks

Globa

ls

Code

⇒Yes.EmphaKcallyyes

SustainableMemoryManagement

Whentheheaprunsoutofspace,thesystemcopes•  Scourstheheaplookingforobjectsthatarenolongerofinterest–  Technicaltermis“live”–  Anobjectisconsideredliveiffitcanbereachedfromtherunningcode

•  Startfromallthenamesintherunningcode–  Variablesareonthestack1–  Globalnamessuchasdeclaredorimportedclasses–  EachobjectonthestackhasadeclaraKonwhichrevealsitsstructure–  Youcanimaginechasingdownchainsofreferencestofindallliveobjects2

➝  That’showitwasdoneforalong&me…

•  Moderngarbagecollectorsaremorenuanced–  TheysKllstartfromthebeginning:local&globalnames–  Mostmoderncollectorsare“copying”collectors

COMP412,SupplementalMaterial

AKA“GarbageCollecWon”

1Localsofthecurrentmethodareonthestack.Localsofthemethodthatcalleditarebelowthecurrentmethodonthestack.Localsofthemethodthatcalledthatmethodarebelow…,andsoon.That’swhytherunKmeusesastack2H.SchorrandW.MWaite,“Anefficientmachine-independentprocedureforgarbagecollecKoninvariousliststructures,Comm.ACM,10(8),1967,pages501—506 7

GarbageCollecKonviaCopying

CopyingCollectors•  Acopyingcollectordividestheheapintotwoormorepools•  Newobjectsareallocatedinthecurrentpool•  Whenthecurrentpoolisfull,execuKonpausesandthecollector:–  copiesallliveobjectsfromthecurrentpooltotheemptypool–  swapsthedesignaKonscurrentandemptyUnreachableobjectsarenotcopied,sothenewpoolhasfreespace

COMP412,SupplementalMaterial 8

HEAP

CurrentPool EmptyPool

1 4

53

2

6

BEFORE

GarbageCollecKonviaCopying

CopyingCollectors•  Acopyingcollectordividestheheapintotwoormorepools•  Newobjectsareallocatedinthecurrentpool•  Whenthecurrentpoolisfull,execuKonpausesandthecollector:–  copiesallliveobjectsfromthecurrentpooltotheemptypool–  swapsthedesignaKonscurrentandemptyUnreachableobjectsarenotcopied,sothenewpoolhasfreespace

COMP412,SupplementalMaterial 9

HEAP

CurrentPool EmptyPool

1 4

53

2

6

Objectsle[intheemptypoolarediscardedenmasse HE

AP

EmptyPool CurrentPool

1 4 532 6

BEFORE

AFTER

Ifyouwantperformance,paya\enWontogarbage•  Collectorlocatesliveobjectsbywalkingoutfromvariables–  Whenyouaredonewithanobject,setthevariabletoNULL–  Leavingthereferencetotheheapobjectwillkeepitlive

•  Storagecan“leak”,orbecomeun-recyclable–  Leaveapointertoalargedatastructureonthestack,orinaglobal,…–  orforgo[eninanotherobject,thathappenstobelive–  LeadstoextracollecKonsand,eventually,anoutofmemoryerror

ImplicaKonsforProgramming

COMP412,SupplementalMaterial 10

Thisisthetakeawaymessage!

10Heap

Stack

“Helloworld!”

StringPoolGlobals

Ifperformancereallyma\ers,paya\enWontosizeofthepool–  Javausesaslightlymorecomplexcopyingcollector–  AllnewobjectsareallocatedintoEden–  Edeniscopied,whenfull,intooneofStable0orStable1–  WhenStableistoofull,itisaddedtotheLongTermPool

InCOMP412,weoffereda5%bonusforthefastestlab1inalanguage•  IntheJavalabs,thetopthreeorfourwereseparatedbythebehaviorofthegarbagecollector•  ThefastestlabhadnomajorcollecKons,&fewerminorcollecKons

HEAP

Eden Stable0 Stable1 LongTermPool

MinorcollecWonMajorcollecWon

ImplicaKonsforProgramming

COMP412,SupplementalMaterial 11

majorminor

swap

COMP412,SupplementalMaterial 12

Doesthisstuffma[er?

PerformanceofOneStudent’sJavaCode,COMP412,Lab1

0.000

5.000

10.000

15.000

20.000

25.000

30.000

- 25,000.00 50,000.00 75,000.00 100,000.00 125,000.00

Standard

BigHeap

SmallHeap

majorcollecWon(2xinspeed)

waytoomanycollecWons


Recommended