Cool New Features in JDK7 update 4!

JDK7 was quite recently released, and I thought I should mention a few new cool things in it.

JCMD

First out is the port of the small JRockit command line utility JRCMD (JRockit Command). JRCMD was a command line tool to enumerate the Java processes running on the local machine, and to send commands (referred to as “Diagnostic Commands”) to them. JRCMD has been renamed JCMD (Java Command).

To list the locally running JVMs, simply run JAVA_HOME\bin\jcmd:

C:\Java\JDKs\JDK7_u4_official\bin>.\jcmd
4636 sun.tools.jconsole.JConsole
2620 HotspotKeepAlive
1076 sun.tools.jcmd.JCmd

You will see the JVMs running on the local machine listed by PID followed by main class. To see the commands available, run the help command, like this:

C:\Java\JDKs\JDK7_u4_official\bin>.\jcmd 4636 help
4636:
The following commands are available:
VM.commercial_features
ManagementAgent.stop
ManagementAgent.start_local
ManagementAgent.start
Thread.print
GC.class_histogram
GC.heap_dump
GC.run_finalization
GC.run
VM.uptime
VM.flags
VM.system_properties
VM.command_line
VM.version
help

For more information about a specific command use ‘help <command>’.

To get information about a specific command, use the help command followed by the command name:

C:\Java\JDKs\JDK7_u4_official\bin>.\jcmd 4636 help Thread.print
4636:
Thread.print
Print all threads with stacktraces.

Impact: Medium: Depends on the number of threads.

Syntax : Thread.print [options]

Options: (options must be specified using the <key> or <key>=<value> syntax)
        -l : [optional] print java.util.concurrent locks (BOOLEAN, false)

To get a thread stack dump with the concurrent locks:

C:\Java\JDKs\JDK7_u4_official\bin>.\jcmd 4636 Thread.print -l
4636:
2012-04-27 11:21:57
Full thread dump Java HotSpot(TM) Client VM (23.0-b21 mixed mode, sharing):

"DestroyJavaVM" prio=6 tid=0x003fb000 nid=0xc5c waiting on condition [0x00000000
]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
        – None

"AWT-EventQueue-0" prio=6 tid=0x03e65800 nid=0x128c waiting on condition [0x041c
f000]
   java.lang.Thread.State: WAITING (parking)
        at sun.misc.Unsafe.park(Native Method)
        – parking to wait for  <0x2926dce8> (a java.util.concurrent.locks.Abstra
ctQueuedSynchronizer$ConditionObject)
        at java.util.concurrent.locks.LockSupport.park(LockSupport.java:186)
        at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject
.await(AbstractQueuedSynchronizer.java:2043)
        at java.awt.EventQueue.getNextEvent(EventQueue.java:511)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
ad.java:213)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
java:163)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:151)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)

        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)

        at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

   Locked ownable synchronizers:
        – None

"Thread-2" prio=6 tid=0x03e61c00 nid=0xd4c in Object.wait() [0x042bf000]
   java.lang.Thread.State: TIMED_WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        – waiting on <0x292432b0> (a java.io.PipedInputStream)
        at java.io.PipedInputStream.read(PipedInputStream.java:327)
        – locked <0x292432b0> (a java.io.PipedInputStream)
        at java.io.PipedInputStream.read(PipedInputStream.java:378)
        – locked <0x292432b0> (a java.io.PipedInputStream)
        at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
        at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
        at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
        – locked <0x29249358> (a java.io.InputStreamReader)
        at java.io.InputStreamReader.read(InputStreamReader.java:184)
        at java.io.BufferedReader.fill(BufferedReader.java:154)
        at java.io.BufferedReader.readLine(BufferedReader.java:317)
        – locked <0x29249358> (a java.io.InputStreamReader)
        at java.io.BufferedReader.readLine(BufferedReader.java:382)
        at sun.tools.jconsole.OutputViewer$PipeListener.run(OutputViewer.java:10
9)

   Locked ownable synchronizers:
        – None

.

.

.
JNI global references: 528

Java Flight Recorder

The second big feature is the first port of the JRockit Flight Recorder – a high performance event recorder built into the JVM. Think of it as the data flight recorder (black box) of an aircraft. It records information about the JVM, its environment and the application running in the JVM, so that, if something happens, clues about what caused the problem can be found. Now, the first port of the flight recorder is quite limited in terms of the events actually being recorded. It is really released to be used with the event producer built into WebLogic Server through WLDF. Also, the Mission Control client is not yet released as part of the JDK (since it is not that useful without WLDF yet). If you’re an Oracle WebLogic customer, you can download the client on MOS.

That being said, to enable the functionality in 7u4, you need to add the following command line flags to your JVM startup command:

JAVA_HOME\bin\java -XX:+UnlockCommercialFeatures -XX:+FlightRecorder <your main class and params>

You can see that it has been enabled by using JCMD. A few flight recorder specific commands should now be available:

C:\Java\JDKs\JDK7_u4_official\bin>.\jcmd 2620 help
2620:
The following commands are available:
JFR.stop
JFR.start
JFR.dump
JFR.check
VM.commercial_features
ManagementAgent.stop
ManagementAgent.start_local
ManagementAgent.start
Thread.print
GC.class_histogram
GC.heap_dump
GC.run_finalization
GC.run
VM.uptime
VM.flags
VM.system_properties
VM.command_line
VM.version
help

For more information about a specific command use ‘help <command>’.

Unless you are a WLS user, I’d personally recommend waiting a release or two before starting using the Flight Recorder. There are not that many OS/JVM/JDK level events available yet, and there is a major performance improvement related to the time stamping of events just around the corner.

4 Responses to "Cool New Features in JDK7 update 4!"

  1. […] of the reactions to my recent blog on JCMD has been “But, why?”. It is a reasonable question – after all, the simple examples I added to […]

  2. […] line after the fact that you have started the JVM, that is possible too. JCMD, which I described in a previous blog, has a set of commands dedicated to controlling the Flight […]

  3. […] Cool New Features in JDK7 update 4! […]

Leave a Reply

Your email address will not be published.