加入VIP
  • 专属下载特权
  • 现金文档折扣购买
  • VIP免费专区
  • 千万文档免费下载

上传资料

关闭

关闭

关闭

封号提示

内容

首页 Twitter的JVM性能调优经验分享(Attila Szegedi)

Twitter的JVM性能调优经验分享(Attila Szegedi).pdf

Twitter的JVM性能调优经验分享(Attila Szeg…

zjznet
2011-10-26 0人阅读 举报 0 0 暂无简介

简介:本文档为《Twitter的JVM性能调优经验分享(Attila Szegedi)pdf》,可适用于IT/计算机领域

AttilaSzegedi,SoftwareEngineeraszEverythingIeverlearnedaboutJVMperformancetuningtwitterEverythingMorethanIeverwantedtolearnedaboutJVMperformancetuningtwitter•Memorytuning•CPUusagetuning•Lockcontentiontuning•IOtuningTwitter’sbiggestenemyCClicensedimagefromhttp:wwwflickrcomphotosdunechaserLatencyLatencycontributors•Byfarthebiggestcontributorisgarbagecollector•othersare,innoparticularorder:•inprocesslockingandthreadscheduling,•IO,•applicationalgorithmicinefficienciesAreasofperformancetuning•Memorytuning•Lockcontentiontuning•CPUusagetuning•IOtuningAreasofmemoryperformancetuning•Memoryfootprinttuning•Allocationratetuning•GarbagecollectiontuningMemoryfootprinttuning•SoyougotanOutOfMemoryError…•Maybeyoujusthavetoomuchdata!•Maybeyourdatarepresentationisfat!•Youcanalsohaveagenuinememoryleak…Toomuchdata•Runwithverbosegc•Observenumbersin“FullGC”messagesFullGC$before>$after($total),$timesecs•CanyougivetheJVMmorememory•DoyouneedallthatdatainmemoryConsiderusing:•aLRUcache,or…•softreferences*Fatdata•Canbeaproblemwhenyouwanttodowackythings,like•loadthefullTwittersocialgraphinasingleJVM•loadallusermetadatainasingleJVM•SlimminginternaldatarepresentationworksattheseeconomiesofscaleFatdata:objectheader•JVMobjectheaderisnormallytwomachinewords•That’sbytes,orbitsonabitJVM!•newjavalangObject()takesbytes•newbytetakesbytesFatdata:padding•newA()takesbytes•newB()takesbytesclassA{bytex}classBextendsA{bytey}Fatdata:noinlinestructs•newC()takesbytes•similarly,noinlinearrayelementsclassC{Objectobj=newObject()}Slimmingtakentoextreme•Aresearchprojecthadtoloadthefullfollowergraphinmemory•Eachvertex’sedgesendedupbeingrepresentedasintarrays•Ifitgrowsfurther,wecanconsidervariablelengthdifferentialencodinginabytearrayCompressedobjectpointers•Pointersbecomebyteslong•UsablebelowGBofmaxheapsize•AutomaticallyusedbelowGBofmaxheapCompressedobjectpointersUncompressedCompressedbitPointerObjectheader*ArrayheaderSuperclasspad*ObjectcanhavebytesoffieldsandstillonlytakeupbytesAvoidinstancesofprimitivewrappers•HardwonexperiencewithScala:•aSeqIntstoresjavalangInteger•anArrayIntstoresint•firstneeds(*length)bytes•secondneeds(*length)bytesAvoidinstancesofprimitivewrappers•ThiswasfixedinScala,butitshowsthat:•youoftendon’tknowtheperformancecharacteristicsofyourlibraries,•andwon’teverknowthemuntilyourunyourapplicationunderaprofilerMapfootprints•GuavaMapMakermakeMap()takesbytes!•MapMakerconcurrencyLevel()makeMap()takesbytes!•ConcurrentMapwithlevelmakessensesometimes(ieyoudon’twantaConcurrentModificationException)Thriftcanbeheavy•Thriftgeneratedclassesareusedtoencapsulateawiretranferformat•Usingthemasyourdomainobjects:almostneveragoodideaThriftcanbeheavy•EveryThriftclasswithaprimitivefieldhasajavautilBitSetissetbitvectorfield•ItaddsbetweenandbytesofoverheadperobjectThriftcanbeheavyThriftcanbeheavy•Thriftdoesnotsupportbitfloats•Couplingdomainmodelwithtransport:•resistancetochangedomainmodel•YoualsomissoportunitiesforinterningandNtonormalizationclasspublicStringcitypublicStringregionpublicStringcountryCodepublicintmetropublicList<String>placeIdspublicdoublelatpublicdoublelonpublicdoubleconfidenceLocation{classpublicStringcitypublicStringregionpublicStringcountryCodepublicintmetropublicList<String>placeIdspublicdoublelatpublicdoublelonpublicdoubleconfidenceLocation{SharedclassUniqueLocation{privateSharedLocationsharedLocationCarefulwiththreadlocals•Threadlocalsstickaround•Particularlyproblematicinthreadpoolswithm⨯nresourceassociation•pooledthreadsusingconnections:youendupwithconnectionbuffers•Considerusingsynchronizedobjects,or•justcreatenewobjectsallthetimePartII:fightinglatencyPerformancetradeoffMemoryTimeConvenient,butoversimplifiedviewPerformancetriangleMemoryfootprintThroughputLatencyPerformancetriangleCompactnessThroughputResponsivenessC⨯T⨯R=a•Tuning:varyC,T,Rforfixeda•Optimization:increaseaPerformancetriangle•Compactness:inverseofmemoryfootprint•Responsiveness:longestpausetheapplicationwillexperience•Throughput:amountofusefulapplicationCPUworkovertime•Cantradeonefortheother,withinlimits•IfyouhavespareCPU,canbepurewinResponsivenessvsthroughputBiggestthreattoresponsivenessintheJVMisthegarbagecollectorMemorypoolsEdenSurvivorOldPermanentCodecacheThisisentirelyHotSpotspecific!HowdoesyounggenworkEdenSOldS•Allnewallocationhappensineden•Itonlycostsapointerbump•Whenedenfillsup,stoptheworldcopycollectionintothesurvivorspace•Deadobjectscostzerotocollect•Aftrseveralcollections,survivorsgettenuredintooldgenerationIdealyounggenoperation•Bigenoughtoholdmorethanonesetofallconcurrentrequestresponsecycleobjects•Eachsurvivorspacebigenoughtoholdactiverequestobjectstenuringones•TenuringthresholdsuchthatlonglivedobjectstenurefastOldgenerationcollectors•Throughputcollectors•XX:UseSerialGC•XX:UseParallelGC•XX:UseParallelOldGC•Lowpausecollectors•XX:UseConcMarkSweepGC•XX:UseGGC(can’tdiscussithere)Adaptivesizingpolicy•Throughputcollectorscanautomaticallytunethemselves:•XX:UseAdaptiveSizePolicy•XX:MaxGCPauseMillis=…(ie)•XX:GCTimeRatio=…(ie)AdaptivesizingpolicyatworkChooseacollector•Bulkservice:throughputcollector,noadaptivesizingpolicy•Everythingelse:trythroughputcollectorwithadaptivesizingpolicyIfitdidn’twork,useconcurrentmarkandsweep(CMS)Alwaysstartwithtuningtheyounggeneration•EnableXX:PrintGCDetails,XX:PrintHeapAtGC,andXX:PrintTenuringDistribution•Watchsurvivorsizes!You’llneedtodetermine“desiredsurvivorsize”•There’snosuchthingasa“desirededensize”,mindyouThebigger,thebetter,withsomeresponsivenesscaveats•WatchthetenuringthresholdmightneedtotuneittotenurelonglivedobjectsfasterXX:PrintHeapAtGCHeapafterGCinvocations=(full):parnewgenerationtotalK,usedKedenspaceK,usedfromspaceK,usedtospaceK,usedconcurrentmarksweepgenerationtotalK,usedKconcurrentmarksweeppermgentotalK,usedK}XX:PrintTenuringDistributionDesiredsurvivorsizebytes,newthreshold(max)age:bytes,totalage:bytes,totalage:bytes,totalage:bytes,total•Thingsofinterest:•Numberofages•Sizedistributioninages•YouwantstronglydecliningTuningtheCMS•Giveyourappasmuchmemoryaspossible•CMSisspeculativeMorememory,lesspunitivemiscalculations•TryusingCMSwithouttuningUseverbosegcandXX:PrintGCDetails•Didn’tgetany“FullGC”messagesYou’redone!•Otherwise,tunetheyounggenerationfirstTuningtheoldgeneration•Goals:•Keepthefragmentationlow•AvoidfullGCstops•Fortunately,thetwogoalsarenotconflictingTuningtheoldgeneration•Findtheminimumandmaximumworkingsetsize(observe“FullGC”numbersunderstablestateandunderload)•Overprovisionthenumbersby•ThisgivesCMSacushiontoconcurrentlycleanmemoryasit’susedTuningtheoldgeneration•SetXX:InitiatingOccupancyFractiontobetween,respectively•correspondstooverprovisionedheapratio•YoucanlowerinitiatingoccupancyfractiontoifyouhaveCPUtospareResponsivenessstillnotgoodenough•ToomanyliveobjectsduringyounggenGC:•ReduceNewSize,reducesurvivorspaces,reducetenuringthreshold•Toomanythreads:•Findtheminimalconcurrencylevel,or•splittheserviceintoseveralJVMsResponsivenessstillnotgoodenough•DoestheCMSabortableprecleanphase,well,abort•Itissensitivetonumberofobjectsinthenewgeneration,so•goforsmallernewgeneration•trytoreducetheamountofshortlivedgarbageyourappcreatesPartIII:let’stakeabreakfromGCThreadcoordinationoptimization•Youdon’thavetoalwaysgoforsynchronized•Synchronizationisareadbarrieronentrywritebarrieronexit•Sometimesyouonlyneedahalfbarrierieinaproducerobserverpattern•VolatilescanbeusedashalfbarriersThreadcoordinationoptimization•Foratomicupdateofasinglevalue,youonlyneedAtomic{Integer|Long}compareAndSet()•YoucanuseAtomicReferencecompareAndSet()foratomicupdateofcompositevaluesrepresentedbyimmutableobjectsFightCMSfragmentationwithslaballocators•CMSdoesn’tcompact,soit’spronetofragmentation,whichwillleadtoastoptheworldpause•ApacheCassandrausesaslaballocatorinternallyCassandraslaballocator•MBslabsizes•copybyteintothemusingcompareandset•GCbefore:secondseveryhour•GCafter:secondsonceindaysandhoursSlaballocatorconstraints•Worksforlimitedusage:•Buffersarewrittentolinearly,flushedtodiskandrecycledwhentheyfillup•Theobjectsneedtobeconvertedtobinaryrepresentationanyway•Ifyouneedrandomfreeingandcompaction,you’reheadingdownthewrongdirection•Ifyoufindyourselfwritingafullmemorymanagerontopofbytebuffers,stop!Softreferencesrevisited•SoftreferenceclearingisbasedontheamountoffreememoryavailablewhenGCencountersthereference•Bydefinition,throughputcollectorsalwaysclearthem•CanusethemwithCMS,buttheyincreasememorypressureandmakethebehaviorlesspredictable•NeedtwoGCcyclestogetridofreferencedobjectsEverythingMorethanIeverwantedtolearnedaboutJVMperformancetuningtwitterQuestionsAttilaSzegedi,SoftwareEngineerasz

用户评价(0)

关闭

新课改视野下建构高中语文教学实验成果报告(32KB)

抱歉,积分不足下载失败,请稍后再试!

提示

试读已结束,如需要继续阅读或者下载,敬请购买!

文档小程序码

使用微信“扫一扫”扫码寻找文档

1

打开微信

2

扫描小程序码

3

发布寻找信息

4

等待寻找结果

我知道了
评分:

/12

Twitter的JVM性能调优经验分享(Attila Szegedi)

仅供在线阅读

VIP

在线
客服

免费
邮箱

爱问共享资料服务号

扫描关注领取更多福利