Tải bản đầy đủ - 0 (trang)
Chapter 18. Profiling and Monitoring Java Applications Using the Sun JDK Tools

Chapter 18. Profiling and Monitoring Java Applications Using the Sun JDK Tools

Tải bản đầy đủ - 0trang

stated,thetoolsusedrefertotheJava6versions.



Chapter18.ProfilingandMonitoring

JavaApplicationsUsingtheSunJDK

Tools

TheSunJDKProfilingandMonitoringTools

ConnectingToandMonitoringaJavaApplicationwithjConsole

MonitoringaRemoteTomcatApplicationwithjConsole

DetectingandIdentifyingMemoryLeakswiththeJDKTools

DiagnosingMemoryLeaksUsingHeapDumps,jmap,andjhat

DetectingDeadlocks



18.1.TheSunJDKProfilingand

MonitoringTools

IfyouareusingJava5orbetter,someofthemostreadily

availableprofilingtoolscomebundledwithyourJDK.TheJava

MonitoringandManagementConsoletool,alsoknownas

jConsole,canbeavaluableaideinmonitoringyourapplications

andidentifyingperformanceissues.ThejConsoletoolhasalot

goingforitasafirstlineperformanceprofilingtool:itisreadily

availableinanyrecentJDKdistribution,youdon'tneedto

instrumentormodifyyourcodeinanyway,andyoucanrunit

withaminimumofconfigurationagainstlocalorremoteJava

applications.Heapanalysistoolssuchasjhathelpyouidentify

andtrackdownmemoryleaks.

Notethatthetoolswediscusshererelateinparticulartothe

SunJDK.AlthoughtheymayworkwithotherJVMs,suchas

BEA'sJRockitandtheIBMvirtualmachines,theseJVMsusually

havetheirownmorespecificprofilingtools.

ThefollowingarticlesdiscusswaysthatyoucanusejConsoleto

monitorandanalyzeJavaapplicationperformanceonyourown

localmachineandalsoonremoteservers.Whennototherwise



stated,thetoolsusedrefertotheJava6versions.



18.2.ConnectingToandMonitoringa

JavaApplicationwithjConsole

ArguablyoneofthemostusefuloftheJDKtools,JConsoleisa

graphicaltoolthatusesJMXtomonitorandreportonthe

activitiesandresourceuseofJavaapplications.Thissection

explainshowtoconnecttoandmonitoraJavaapplication

runningeitherlocallyoronanotherserver.

JConsoleworkswithapplicationsrunningunderJava5orJava

6.Java5comesbundledwithJMX1.2,butyoudoneedto

activatetheJMXAgentatruntimewhenyoustartthe

applicationthatyouwanttomonitor.Tomonitoranapplication

locally,youneedtospecifythe(rathercounterintuitively

named)com.sun.management.jmxremoteJavasystem

property,asshownhere:

$java-Dcom.sun.management.jmxremote-jarmyapp.jar

InJava6,itismucheasiertoconnecttolocalJavaapplication

withJConsole.TheJava6versionofjConsolecandynamically

connecttoandmonitoranylocalapplicationrunninginaJava6

VM.JConsoledynamicallyactivatestheJMXmanagementagent

inthetargetVM,soyounolongerneedtostartthemonitored

applicationinanyparticularway.Thisisveryusefulfor

analyzingperformanceissuesinlocallyrunningapplications

withminimumeffort.

Althoughthisisthemostconvenientwaytomonitoran

application,youshouldnotuseittomonitorapplications

runningonaproductionserver.JConsolecanbedemandingin

termsofbothmemoryandCPU,soyoushouldrunitona

separatemachine(forexample,yourdevelopment

workstation),connectingtothetargetVMoverthenetwork.

JConsolecandothisperfectlywell,bothinJava5andJava6,

butitrequiressomeconfigurationofthetargetVM.Youneedto

startthetargetJVMwith(atleast)the



com.sun.management.jmxremote.portJavasystemproperty,

whichspecifiestheporttobeusedforJMXmonitoring:



$java-Dcom.sun.management.jmxremote.port=3000-jarmyAppInPro

ThiswillenableyoutomonitortheapplicationviaJMX/RMI

throughthe3000port.

Inarealproductionenvironment,accesswilltypicallybe

securedandyouwillusuallyneedtoprovideausernameand

password.Thisisfairlyeasytosetupandiswelldocumentedin

theSundocumentation,sowewon'tbecoveringithere.Just

beawarethatyouwillprobablyneedauseraccountanda

cooperativesystemadministratortomonitoraJavaapplication

onaproductionbox.

YoustartjConsolefromthecommandlineasfollows:

$jconsole

ThiswillopenawindowlistingtheJMX-compatibleapplications

currentlyrunningonthelocalmachine(seeFigure18-1).

Alternatively,ifyouneedtoconnecttoanapplicationrunning

onaremoteserver,youcanspecifytheaddressofaremote

machinealongwiththeJMXportforthisapplication.

Figure18-1.StartingJConsole



InsomeWindowsenvironments,theJavaprocessnamesare

notveryinformative,andonlytheprocessIDsaredisplayed.In

thesecases,youmayhavetroubleworkingoutexactlywhat

processyouaresupposedtobemonitoring.Knowingthe

processIDyouwantcanbeagreathelp.Inaddition,ifyou

knowtheprocessIDofthetaskyou'reafter,youcansaveabit

oftimeusingthisprocessIDtoconnectdirectlytothe

applicationfromthecommandline.OnaUnixsystem,thisis

fairlyeasytodousingthepscommand,asinthefollowing

example:

$psaux|grepjava

Anothercommand-linetoolfordiscoveringJavaprocessIDsis

jps,whichliststhecurrentJavaprocess(forthecurrentuser):



C:\>jps-l

984com.xmlmind.xmleditapp.start.Start

3068org.apache.catalina.startup.Bootstrap

3108sun.tools.jps.Jps

$jconsole3068

Thejpscommand-linetoolisconvenient,andithasthe

advantageofrunningonallplatforms.However,itdoesn't

alwaysdisplayalltheJavaprocessesrunningonaWindows

machine.ItwillnotpickupaTomcatinstancerunasaservice

onaWindowsserver,forexample,iftheservicehasbeen

startedunderanotheraccount.Infact,inaWindows

environment,amorereliablealternativeistoidentifythe

applicationinstanceusingtheWindowsTaskManager.

ItisfairlyeasytousetheWindowstaskmanagertodetermine

theprocessIDoftheapplicationyouwanttomonitor.Todo

this,startuptheWindowstaskmanager,andopenthe"View

SelectColumns..."menu.ThencheckthePIDboxsothat

processIDsaredisplayedaswellastheapplicationnames.

Now,intheProcessestab,youwillbeabletoseetheprocessid

(PID)ofeachcurrentlyrunningapplication(seeFigure18-2).

Figure18-2.StartingJConsoleprocessestab



Youcanalsostartmonitoringaremoteapplicationdirectlyfrom

thecommandline.JustgivejConsoletheserveraddressand

theJMXport,asshownhere:

$jconsolemy.production.server:8086

Onceconnected,thejConsoleapplicationdisplaysasummaryof

thecurrentstateofthetargetapplication(seeFigure18-3).

Laterinthischapter,wewillseehowtousetheseviewsto

analyzethebehaviorofyourapplication.

Figure18-3.MonitoringaJavaapplicationwithJConsole



18.3.MonitoringaRemoteTomcat

ApplicationwithjConsole

OnecommonuseofjConsoleistomonitoranapplication

deployedonaJavaservletcontainerorapplicationserversuch

asTomcatorJBoss.Inthissection,wewillgothroughhowto

configureandmonitoraTomcatserverusingjConsole.The

generalapproach—and,inparticular,thescript-basedapproach

—issimilarforotherapplicationservers.

Aswenotedearlier,forobvioussecurityreasons,youcan'tjust

connecttoanyoldTomcatinstanceonaremoteserver;the

serverhastobeconfiguredcorrectlytoallowremote

monitoringoftheTomcatinstance.InaUnixenvironment,you

needtomodifytheTomcatstartupscripttoenableJMX

monitoring.ThissimplyinvolvesintegratingtheappropriateJMX

JavaSystempropertiesintotheTomcatstartupprocess.In

Tomcat,youcaneithermodifytheJAVA_OPTSvariableorthe

CATALINA_OPTSvariable.ModifyingtheJAVA_OPTSvariable

willalsoworkwithmostotherJavaapplicationservers.Inthis

example,weactivateJMXonport8086,and(forsimplicity)

deactivateSSLauthentication,whichwouldotherwisebe

activatedbydefault:



JAVA_OPTS="$JAVA_OPTS"-Dcom.sun.management.jmxremote"\

"-Dcom.sun.management.jmxremote.port=8086

"-Dcom.sun.management.jmxremote.ssl=false

"-Dcom.sun.management.jmxremote.authentic



InaWindowsenvironmentwhereTomcathasbeeninstalledas

aservice,openthe"ApacheTomcat ConfigureTomcat"menu

ontheserver,orrunthetomcat5wapplicationwhichcanbe

foundintheTomcatbindirectory.GototheJavatabandadd

thefollowingoptionsattheendofthelistofusualoptions(see

Figure18-4):

-Dcom.sun.management.jmxremote



-Dcom.sun.management.jmxremote.port=8086

-Dcom.sun.management.jmxremote.ssl=false

-Dcom.sun.management.jmxremote.authenticate=false



Figure18-4.ConfiguringTomcattoenableJMX

monitoring



Backonyourdevelopmentmachine,youcannowconnectto

theTomcatinstancebychoosingthe"RemoteProcess"optionin

the"NewConnection"dialogbox(seeFigure18-5),specifying

theremoteserverURLandport,aswellastheusernameand

passwordifneededforasecureconnection.

Figure18-5.ConfiguringTomcattoenableJMX



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Chapter 18. Profiling and Monitoring Java Applications Using the Sun JDK Tools

Tải bản đầy đủ ngay(0 tr)

×