The Source for Java Technology Collaboration
User: Password:



Eamonn McManus

Eamonn McManus's Blog

Mustang JDK now includes attach-on-demand

Posted by emcmanus on September 29, 2005 at 09:32 AM | Comments (8)

The Tiger JDK introduced a nifty feature whereby you could run an application with -Dcom.sun.management.jmxremote and then later connect to it using the jconsole tool. That allows you to see the JVM's instrumentation (memory, classes, threads) plus any JMX instrumentation you've defined yourself. As Alan Bateman describes, with the Mustang JDK you can now attach jconsole to any application, even if it wasn't launched with the magic -Dcom.sun.management.jmxremote incantation.

This is excellent news, for a few reasons. First of all, what a bummer if you suddenly realize after your uninterruptible app has been running for five days that you really want to get at its instrumentation, but damn, you forgot to incant -Dcom.sun.management.jmxremote! Not a problem any more. You can connect to any of your JVM processes from a jconsole on the same machine. (You can't connect to anyone else's processes, and you can't connect remotely in this way.)

Secondly, there's a small penalty in start-up time and in footprint when you launch with the incantation. On the old slow workstation I'm typing this on, this is almost three seconds and 500K. This isn't enormous, but it's not negligible either. So why not wait to pay the penalty until you actually need to, which might be never?

And finally, enabling this connection, even from the local machine, means creating and listening on a TCP/IP socket. If you're not going to be using it then why bother?

So the new feature is a big plus! If you want to play with it, you can download the latest Mustang snapshot at http://download.java.net/jdk6/binaries/. Both jconsole and the target app must be on Mustang.

By the way, Alan modestly doesn't mention the deep technical wizardry he needed to do to get this to work. Causing a target process to execute code for you (to create the JMX connector server) isn't a piece of cake!


Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • Excellent feature. BTW, is JMX re-entrant? I would like to use JConsole to monitor JDistro, both running in the same JVM. It seems not possible. Heisenberg here?

    Posted by: desnoix on September 29, 2005 at 03:31 PM

  • I'm not familiar with JDistro and can't say offhand what is happening here. JConsole can connect to its own MBeanServer (i.e. the Platform MBeanServer) and see any MBeans there, including MBeans put there by someone else. To do this, use the Remote tab in the connection dialog and specify host localhost and port 0.

    I used the following test program to check this. You need to compile and run with jconsole.jar and jtools.jar from the <JDK_HOME>/lib directory in your classpath.

    import java.lang.management.*;
    import javax.management.*;
    import sun.tools.jconsole.*;
    
    public class RunJConsole {
        public static void main(String[] args) throws Exception {
            MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
            mbs.registerMBean(new MBeanServerDelegate(), new ObjectName("a:b=c"));
            JConsole.main(new String[0]);
        }
    }
    

    As usual with sun.* classes, there's no guarantee that this program will continue to work, because sun.* classes can change incompatibly between releases.

    Posted by: emcmanus on September 30, 2005 at 01:05 AM

  • Thank you for your quick answer. My question was general (not specific to JDistro). And the news is excellent. I have tried it and it works in a normal JVM. I also tried it in JDistro, JConsole starts and I can see the mbean too. But I have an exception at startup. Any idea?
    Best regards, Guillaume
    (I hope you don't mind about the exception here)
    
    java.vm.vendor=Sun Microsystems Inc.
    java.vm.version=1.5.0_03-b07
    os.arch=i386
    os.name=Linux
    os.version=2.6.11-6mdk
    
    java.lang.InternalError: The size of array arguments itemNames[] and
    itemValues[] should be equal to the number of items defined in argument
    compositeType (found 4 elements in itemNames[] and itemValues[], expecting 5
    elements according to compositeType.
    	at sun.management.Util.newInternalError(Util.java:41)
    	at sun.management.MemoryUsageCompositeData.getCompositeData(MemoryUsageCompositeData.java:53)
    	at sun.management.MemoryUsageCompositeData.toCompositeData(MemoryUsageCompositeData.java:34)
    	at sun.management.MappedMXBeanType$CompositeDataMXBeanType.toOpenTypeData(MappedMXBeanType.java:625)
    	at sun.management.MappedMXBeanType.toOpenTypeData(MappedMXBeanType.java:124)
    	at sun.management.MXBeanSupport.getAttribute(MXBeanSupport.java:466)
    	at sun.management.MXBeanSupport.getAttribute(MXBeanSupport.java:71)
    	at com.sun.jmx.mbeanserver.DynamicMetaDataImpl.getAttribute(DynamicMetaDataImpl.java:96)
    	at com.sun.jmx.mbeanserver.MetaDataImpl.getAttribute(MetaDataImpl.java:181)
    	at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getAttribute(DefaultMBeanServerInterceptor.java:638)
    	at com.sun.jmx.mbeanserver.JmxMBeanServer.getAttribute(JmxMBeanServer.java:659)
    	at sun.management.MXBeanSupport$ProxyMethod.invoke(MXBeanSupport.java:399)
    	at sun.management.MXBeanSupport.forward(MXBeanSupport.java:350)
    	at sun.management.PlatformMXBeanInvocationHandler.invoke(PlatformMXBeanInvocationHandler.java:55)
    	at $Proxy3.getHeapMemoryUsage(Unknown Source)
    	at sun.tools.jconsole.SummaryTab.formatSummary(SummaryTab.java:116)
    	at sun.tools.jconsole.SummaryTab.update(SummaryTab.java:45)
    	at sun.tools.jconsole.VMPanel.update(VMPanel.java:203)
    	at sun.tools.jconsole.VMPanel.access$000(VMPanel.java:19)
    	at sun.tools.jconsole.VMPanel$1.run(VMPanel.java:87)
    	at java.util.TimerThread.mainLoop(Timer.java:512)
    	at java.util.TimerThread.run(Timer.java:462)
    Caused by: javax.management.openmbean.OpenDataException: The size of array
    arguments itemNames[] and itemValues[] should be equal to the number of
    items defined in argument compositeType (found 4 elements in itemNames[]
    and itemValues[], expecting 5 elements according to compositeType.
    	at javax.management.openmbean.CompositeDataSupport.(CompositeDataSupport.java:128)
    	at sun.management.MemoryUsageCompositeData.getCompositeData(MemoryUsageCompositeData.java:48)
    	... 20 more
    

    Posted by: desnoix on September 30, 2005 at 11:02 AM

  • Guillaume, that's quite mysterious. I don't think we've seen that before. Is it possible that the class java.lang.management.MemoryUsage has been modified in some way? Is this an exception you see all the time or just sometimes? Perhaps you could mail me (eamonn dot mcmanus at sun dot com) to explore this further.

    Posted by: emcmanus on October 01, 2005 at 07:14 AM

  • hey hey wait a minute - what is this line?

    > java.vm.version=1.5.0_03-b07

    I thought we are talking about Mustang here?

    Posted by: alexlamsl on December 03, 2005 at 05:02 AM

  • Hi am running Java application but when trying to connect Jconsole of Mustang am getting a message called "management agent cannot be enabled for the selected process" Can yu explain this! and is there any option which i have to use to make my application Observable. Is there any default option which avoids monitoring Java application with profilers.

    Posted by: prinxr on February 13, 2006 at 03:07 AM

  • prinxr, most often when you see "The management agent is not enabled on this process", it means that the process is not running Mustang but some earlier JDK version. Could that be what you are seeing?

    I'm not sure I understand your question about profilers. You want to be able to prevent an application from being profiled? Why?

    Posted by: emcmanus on February 13, 2006 at 06:26 AM





Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds