Tools in the Box Quick overview on helpful tools in the JDK and use cases for them. Florin Bunau dev@tora
http://docs.oracle.com/javase/7/docs/technotes/tools/ - No new tool in Java 7, very few changes in options Basic Tools : Security Tools : Internationalization Tools: Remote Method Invocation (RMI) Tools : Java IDL and RMI-IIOP Tools : Java Deployment Tools : Java Web Start Tools : Java Troubleshooting, Profiling : Monitoring and Management Tools : Java Web Services Tools : Troubleshooting Tools : Scripting Tools appletviewer, apt, extcheck, jar, java, javac, javadoc, javah, javap, jdb keytool, jarsigner, policytool, kinit, klist, ktab native2ascii rmic, rmiregistry, rmid, serialver tnameserv, idlj, orbd, servertool javafxpackager, pack200, unpack200 javaws jconsole, jps, jstat, jstatd, jvisualvm schemagen, wsgen, wsimport, xjc jinfo, jhat, jmap, jsadebugd, jstack jrunscript
start the demo here cd DemoServer javac $(find * grep.java) echo Main-Class: demo.jug.demoserver > manifest.txt jar -cvfm DemoServer.jar manifest.txt $(find * grep.class) java -jar DemoServer.jar
jps JVM Process Status Tool - Lists instrumented HotSpot JVMs on a target system. jps 18027 Java2Demo.JAR 18032 jps - Useful for getting the pid of the JVM. Needed for other commands. - Has options for listing arguments JVM arguments used, program arguments, application name, main class etc..
jstack Stack Trace for Java - Prints a stack trace of threads for a given process or core file or remote debug server Jstack 3299 Java stack information for the threads listed above: ================================================ === "BadRunnablePool--RunnableA": at demo.jug.demoserver$runnablea.run(demoserver.java:20 6) - waiting to lock <0xab789c98> (a java.lang.object - Useful for quickly seeing the current stack. - Can be grep-ed - Detects deadlocks - Can load a dump file.. Found one Java-level deadlock: ============================= "BadRunnablePool--RunnableA": waiting to lock monitor 0x08a65c9c (object 0xab789c98, a java.lang.object), which is held by "BadRunnablePool--RunnableA- RunnableA-RunnableA-RunnableB"
jstat JVM Statistics Monitoring Tool Collects and logs performance statistics as specified by the command line options Statistics on : -class -compiler -gc -gccapacity -gccause -gcnew -gcnewcapacity -gcold -gcoldcapacity -gcpermcapacity -gcutil -printcompilation Behavior of the class loader. Behavior of the HotSpot Just-in-Time compiler. Behavior of the garbage collected heap. Capacities of the generations and their corresponding spaces. Same as -gcutil, with the cause of the last and current Behavior of the new generation. Sizes of the new generations and its corresponding spaces. Behavior of the old and permanent generations. Sizes of the old generation. Sizes of the permanent generation. Summary of garbage collection statistics. HotSpot compilation method statistics.
jstat jstat -gcutil 3436 250 100 S0 S1 E O P YGC YGCT FGC FGCT GCT 46.27 0.00 93.71 5.44 15.05 2 0.010 0 0.000 0.010 46.27 0.00 99.49 5.44 15.05 2 0.010 0 0.000 0.010 0.00 60.73 7.10 5.44 15.05 3 0.011 0 0.000 0.011 0.00 60.73 11.85 5.44 15.05 3 0.011 0 0.000 0.011 0.00 60.73 18.47 5.44 15.05 3 0.011 0 0.000 0.011 0.00 60.73 23.44 5.44 15.05 3 0.011 0 0.000 0.011 0.00 60.73 31.07 5.44 15.05 3 0.011 0 0.000 0.011... -Quickly profile and see what s happening to the JVM -Can grep it, use output for specific project tools
Java Command line debugger jdb -Can connect to JVMs not started in debug-mode, with read-only functionality.! This does not include setting breakpoints. Does include : thread-stack-moving, dumping locals, seeing source code etc.. # connect directly to JVM jdb -connect sun.jvm.hotspot.jdi.sapidattachingconnector:pid=2343 # connect through debug-daemon to JVM (jsadebugd <pid> started first) jdb connect sun.jvm.hotspot.jdi.sadebugserverattachingconnector:debugservername=192.168.1.102 # or to core dump files jdb connect sun.jvm.hotspot.jdi.sacoreattachingconnector:javaexecutable=$java_home/bin/java,core=~/corefile
jdb - Connect to process started in debug java -agentlib:jdwp=transport=dt_socket,address=localhost:3333,server=y,suspend=y -jar DemoServer.jar jdb -connect com.sun.jdi.socketattach:hostname=localhost,port=3333 - see threads - see stack traces, live - stepping - catch exceptions and decide flow -breakpoints -watch points Advantages over IDE : -really fast over far-away servers -quick debugging -can debug without having the source code locally Disadvantages over IDE : -no conditional breakpoints - slow use for daily dev-debug
jmap -Memory Map for Java - Prints shared object memory maps or heap memory details of a given process or core file or a remote debug server - Can be used for obtaining quick info on memory (heap status, GC used) - Most commonly used to obtain heap dump - Dump can be further used by analysis in non-standard profilers too. jmap -dump:format=b,file=dump1 3813 Take a snapshot in some critical moment and analyze it later.!! Pauses the JVM until it is done Why did the servers take so much memory, what objects did we have etc..
jhat Heap Dump Browser Starts a web server on a heap dump file (for example, produced by jmap -dump), allowing the heap to be browsed. Can make all sorts of cool standard queries - All Classes Query - Class Query - Object Query - Instances Query - Roots Query - Reachable Objects Query - Instance Counts for All Classes Query - All Roots Query - Histogram Queries jhat dump1 - New Instances Query (cool diff between dumps)
jhat Can also make custom OQL queries select JavaScript-expression-to-select [ from [instanceof] classname identifier [ where JavaScript-boolean-expression-to-filter ] ] select s from java.util.arraylist s where s.size >= 2000
jvisualvm Java VisualVM combines several monitoring, troubleshooting, and profiling utilities into a single tool. For example, most of the functionality offered by the standalone tools jmap, jinfo, jstat and jstack have been integrated into Java VisualVM. Other functionalities, such as some of those offered by the JConsole tool, can be added as optional plug-ins. java -Dcom.sun.management.jmxremote.port=5555 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Djava.rmi.server.hostname=192.168.1.102 -jar DemoServer.jar jstatd -J-Djava.security.policy=permissions.txt -J-Djava.rmi.server.hostname=192.168.1.102
jvisualvm
jvisualvm
jvisualvm And much more other cool stuff
Thank you for your time http://docs.oracle.com/javase/7/docs/technotes/tools/ http://docs.oracle.com/javase/7/docs/webnotes/tsg/tsg-vm/html/tools.html Further reading and advanced uses of some tools Java Performance - Charlie Hunt (Author), Binu John) Publication Date: October 14, 2011