day2Jrockit.docx
《day2Jrockit.docx》由会员分享,可在线阅读,更多相关《day2Jrockit.docx(20页珍藏版)》请在冰豆网上搜索。
day2Jrockit
Jrockit
时间:
8
1.ExplainOracleJRockitJDK
AbouttheJDK
TheJRockitJVMisahigh-performanceJVMdevelopedtoensurereliability,scalability,manageability,andflexibilityforJavaapplications.TheJRockitJVMprovidesimprovedperformanceforJavaapplicationsdeployedonIntel32-bit(Xeon)and64-bit(XeonandSPARC)architecturesatsignificantlylowercoststotheenterprise.Further,itistheonlyenterprise-classJVMoptimizedforIntelarchitectures,providingseamlessinteroperabilityacrossmultiplehardwareandoperatingsystemconfigurations.TheJRockitJVMenablesyourJavaapplicationstorunoptimallyonWindowsandLinuxoperatingsystems(both32-bitand64-bitarchitectures).TheJRockitJVMisespeciallywell-suitedforrunningOracleWebLogicServer.
TheJRockitJVMisonecomponentoftheOracleJRockitJavadevelopmentkit(JDK).InadditiontotheJRockitJVM,theJDKiscomprisedoftheJavaRuntimeEnvironment(JRE),whichcontainstheJVMandJavaclasslibraries(asspecifiedbytheJavaPlatform,StandardEdition6APISpecification),aswellasasetofdevelopmenttools,suchasacompiler.
TheJRockitJDKiscompatiblewithJ2SE5.0andJavaSE6.
2.UseJITCompilationandOptimizations
just-in-timecompilation(JIT),alsoknownasdynamictranslation,isamethodtoimprovetheruntimeperformanceofcomputerprograms.Traditionally,computerprogramshadtwomodesofruntimeoperation,eitherinterpretedorstatic(ahead-of-time)compilation.Interpretedcodeistranslatedfromahigh-levellanguagetoamachinecodecontinuouslyduringeveryexecution,whereasstaticallycompiledcodeistranslatedintomachinecodebeforeexecution,andonlyrequiresthistranslationonce.
JITcompilersrepresentahybridapproach,withtranslationoccurringcontinuously,aswithinterpreters,butwithcachingoftranslatedcodetominimizeperformancedegradation.Italsooffersotheradvantagesoverstaticallycompiledcodeatdevelopmenttime,suchashandlingoflate-bounddatatypesandtheabilitytoenforcesecurityguarantees.
JITbuildsupontwoearlierideasinrun-timeenvironments:
bytecodecompilationanddynamiccompilation.Itconvertscodeatruntimepriortoexecutingitnatively,forexamplebytecodeintonativemachinecode.
Severalmodernruntimeenvironments,suchasMicrosoft's.NETFrameworkandmostimplementationsofJava,relyonJITcompilationforhigh-speedcodeexecution.
Step1:
JITCompilation
Thefirststepinthemachine-codegenerationprocessisjust-in-time(JIT)compilation.JRockitdoesnotincludeaninterpreter;sotheJITcompilationofthebytecodeintonativemachinecodehastooccurbeforeamethodexecutes.TheJITcompilationisperformedthefirsttimeaJavamethodiscalled.
TheJITcompilerisfastandgeneratesmoderatelyefficientcode.ThisisnecessarytoenabletheJavaapplicationtostartandrunquickly.Subsequently,profilingrevealsfrequentlycalledmethods(hotspots)thatrequirefurtheroptimization.TheJRockitapproach—JITcompilationandnointerpreter—resultsinrelativelylongerstartuptimes,buteveniftheJITcompilationresultsinonlymoderatelyefficientcode,thegeneratedcodeisstillsignificantlyfasterthaninterpretedcode.
Step2:
ThreadMonitoring
Duringthesecondstepofthemachine-codegenerationprocess,theJRockitJVMusesasophisticated,low-cost,samplingbasedtechniquetoidentifywhichfunctionsmeritoptimization:
a"samplerthread"wakesupperiodicallyandchecksthestatusofseveralapplicationthreads.Itidentifieswhateachthreadisexecutingandlogssomeexecutionhistory.Thisinformationistrackedforallthemethods;whentheinformationindicatesthatamethodisheavilyused(hot),thatmethodisearmarkedforoptimization.Usually,aflurryofsuchoptimizationopportunitiesoccurintheapplication'searlyrunstages,withtherateslowingdownasexecutioncontinues.
Step3:
CodeOptimization
Codeoptimizationisaprocessbywhichcommonly-executedcodeisrecompiledtomakeitrunmoreefficiently.
ThefirsttimethattheJRockitJVMrunsamethod,themethodiscompiledintomachinecode.Thiscompilationisquick,buttheresultingcodeisnotasefficientasitcouldbe.Thiscodeisacceptableformethodsthatarerunonceanddiscarded;however,ifamethodisusedrepeatedly,thesystemcangetaperformanceboostifthecodeforthatparticularmethodisregeneratedinamoreefficientway.
TheJRockitJVMoptimizesthesecommonly-executed(hot)methodstomakethecodeasefficientaspossible.Theoptimizationrunsinthebackgroundanddoesnotinterferewiththerunningapplication.
3.ManageMemory
Mnumberof"static"collectorsthatcanbestartedeitherbyspecifyingtheactualcollector(-Xgc:
)orbydefault,asdeterminedbytheJVMmodeyouselectatstartup.
Commandlineoption:
-Xns:
Thesizeofthenurseryhasanimpactonallocationspeed,garbagecollectionfrequencyandgarbagecollectiontimes.Asmallnurserywillbecomefullquicklyandmustbegarbagecollectedmoreoften,whilegarbagecollectionofalargenurserytakesslightlylongertime.Anurserythatissosmallthatfewornoobjectshavediedbeforeayoungcollectionisstartedisofverylittleuse,andneitherisanurserythatissolargethatnoyoungcollectionsareperformedbetweengarbagecollectionsofthewholeheapthataretriggeredduetoallocationoflargeobjectsinoldspace.
SettingaPauseTargetforDeterministicMode
Commandlineoption:
-XpauseTarget:
Thedeterministicmodeusesapausetargetforoptimizingthepausetimes.Thepausetargetimpactstheapplicationthroughput,asalowerpausetargetwillinflictmoreoverheadonthememorymanagementsystem.Setthepausetargetashighasyourapplicationcantolerate.
Thegarbagecollectorwillaimonkeepingthegarbagecollectionpausesbelowthegivenpausetarget.
SettingaPauseTargetforPausetimeMode
Commandlineoption:
-XpauseTarget:
Thepausetimemodeusesapausetargetforoptimizingthepausetimes.Thepausetargetimpactstheapplicationthroughput,asalowerpausetargetwillinflictmoreoverheadonthememorymanagementsystem.Setthepausetargetashighasyourapplicationcantolerate.
4.ManageThreadsandLocks
UnderstandingThreads
AjavaapplicationconsistsofoneormorethreadsthatrunJavacode.TheentireJVMprocessconsistsoftheJavathreadsandsomeJVMinternalthreads,forexampleoneormoregarbagecollectionthreads,acodeoptimizerthreadandoneormorefinalizerthreads.
Fromtheoperatingsystem’spointofviewtheJavathreadsarejustlikeanyapplicationthreads.Schedulingofthethreadsishandledbytheoperatingsystem,aswellasthreadpriorities.
WithinJava,theJavathreadsarerepresentedbythreadobjects.Eachthreadalsohasastack,usedforstoringruntimedata.Thethreadstackhasaspecificsize.Ifathreadtriestostoremoreitemsonthestackthanthestacksizeallows,thethreadwillthrowastackoverflowerror.
UnderstandingLocks
Whenthreadsinaprocessshareandupdatethesamedata,theiractivitiesmustbesynchronizedtoavoiderrors.InJava,thisisdonewiththesynchronizedkeyword,orwithwaitandnotify.Synchronizationisachievedbytheuseoflocks,eachofwhichisassociatedwithanobjectbytheJVM.Forathreadtoworkonanobject,itmusthavecontroloverthelockassociatedwithit,itmust“hold”thelock.Onlyonethreadcanholdalockatatime.Ifathreadtriestotakealockthatisalreadyheldbyanotherthread,thenitmustwaituntilthelockisreleased.Whenthishappens,thereissocalled“contention”forthelock.
Therearefourdifferentkindsoflocks:
•Fatlocks:
Afatlockisalockwithahistoryofcontention(severalthreadstryingtotakethelocksimultaneously),oralockthathasbeenwaitedon(fornotification).
•Thinlocks:
Athinlockisalockthatdoesnothaveanycontention.
●Recursivelocks:
Arecursivelockisalockthathasbeentakenbyathreadseveraltimeswithouthavingbeenreleased.
•Lazylocks:
Alazylockisalockthatisnotreleasedwhenacriticalsectionisexited.Oncealazylockisacquiredbyathread,otherthreadsthattrytoacquirethelockhavetoensurethatthelockis,orcanbe,released.LazylocksareusedbydefaultinOracleJRockitJVM27.6.Inolderreleases,lazylocksareonlyusedifyouhavestartedtheJVMwiththe-XXlazyUnlockingoption.
Athinlockcanbeinflatedtoafatlockandafatlockcanbedeflatedtoathinlock.TheJRockitJVMusesacomplexsetofheuristicstodeterminewhentoinflateathinlocktoafatlockandwhentodeflateafatlocktoathinlock.
LockChains
Severalthreadscanbetiedupinwhatiscalledlockchains.Althoughtheyappearsomewhatcomplex,lockchainsarefairlystraightforward.Theycanbedefinedasfollows:
●ThreadsAandBformalockchainifthreadAholdsalockthatthreadBistryingtotake.IfAisnottryingtotakealock,thenthelockchainis“open.”
●IfA->Bisalockchain,andB->Cisalockchain,thenA->B->Cisamorecompletelockchain.
●IfthereisnoadditionalthreadwaitingforalockheldbyC,thenA->B->Cisacompleteandopenlockchain.
LockChainTypes
TheJRockitJVManalyzesthethreadsandformscompletelockchains.Therearethreepossiblekindsoflockchains:
Open,DeadlockedandBlockedlockchains.
OpenChains
Openlockchainsrepresentastraightdependency,threadA