Java Magazine, Jan/Feb 2017
ORACLE COM JAVAMAGAZINE JANUARY FEBRUARY 2017 31 tools Unfortunately you cannot do much just with read only counters Two way communication with the JVM requires a different technology Dynamic Attach and Instrumentation API The Dynamic Attach mechanism provides the means to connect to a running VM and execute one of several predefined commands You can ask the JVM to dump a Java heap print stack traces change certain VM flags load an agent library and so on The VM executes commands on its own so it must be alive and healthy in order to respond The Java API for Dynamic Attach is available in the same tools jar file Note that this is a vendor specific API applicable only to OpenJDK and Oracles JDK Attaching to a running Java process is straightforward you need to know only the target process ID pid as shown in the following code Dynamic Attach requires no special VM options It can connect to any local HotSpot JVM unless it is started with the XX DisableAttachMechanism flag import com sun tools attach VirtualMachine VirtualMachine vm VirtualMachine attach pid try vm loadAgent agentJarPath options finally vm detach This shows how to inject a Java agent into a running VM A Java agent is a utility program for instrumenting an application It should be packed into a JAR file and contain a class with an agentmain method The instrumentation API enables Java agents to transform the bytecode of existing classes When used together with Dynamic Attach it enables you to change the code of a running application even if the application is started without any debugging facilities Here is a simple agent that installs a new version of MyClass public static void agentmain String args Instrumentation instr throws Exception Class oldClass Class forName org pkg MyClass Path newFile Paths get path to MyClass class byte newData Files readAllBytes newFile instr redefineClasses new ClassDefinition oldClass newData Remember the limitations of the redefineClasses API a new version of a class file cannot add new methods or fields nor can it remove existing members Basically only method bodies can be changed but this is often enough for a hot fix The ability to instrument running Java processes makes the Attach API an important tool for maintenance of enterprise applications The Dynamic Attach mechanism requires full cooperation from the JVM It becomes useless if the JVM has hung or become too busy When this happens it is time to call for brute force such as the serviceability agent Serviceability Agent The HotSpot Serviceability Agent SA provides a low level view of a Java process from a VM perspective It knows everything about Java HotSpot VM internal structures including the heap layout the system dictionaries the compiled code the threads and the stacks Moreover this information is available through a clear and simple Java API so developers can benefit from it without having experience in disassemblers and other hacker facilities The SA was originally invented by Java HotSpot VM engi
You must have JavaScript enabled to view digital editions.