To profile a Java application, the profiler agent should be loaded into the JVM.
There are two ways to do it:
The second approach (attach mode) simplifies profiling, making it even more "on-demand profiling" than ever. Avoid a special step to enable profiling: any running Java application can be profiled.
How to attach to a local application
In Welcome screen, click corresponding element in the list.
The list of applications to attach
(both in UI and when using remote attach wizard)
contains applications detected via standard JVM enumeration mechanism,
as well as applications not detected via standard JVM enumeration mechanism,
such as Tomcat running as Windows service,
running under different user
(in particular, other Java EE servers started as Windows service).
Anyway, it is possible that a Java application which was not started with standard Java launcher
or was started under a different user,
will not be automatically detected and thus will not be shown in the list of locally running applications.
To profile such application, use "Attach by PID"
(you can learn the PID via Task Manager in Windows or
command on non-Windows).
After successful attach, the application will appear in the list.
How to attach to a remote application
Run attach wizard on the remote machine, then use "Connect to remote application..." from Welcome screen.
The attach mode limitations
Unfortunately, the attach mode is not an ultimate solution. The problem is that existing JVMs provide only limited set of profiling capabilities for the attached agents. To get all profiling capabilities, you still have to start the profiled application with the profiler agent instead.
The attach mode is only supported for Sun Java (HotSpot) and JRockit.
Existing IBM VMs do not provide necessary capabilities to let YourKit Java Profiler function in the attach mode. To profile on IBM Java, please start application with the profiler agent. If necessary capabilities are added in future versions of IBM Java, the profiler will support attach mode for IBM Java too.
Attach may fail due to insufficient access rights. For example, it may not be possible to attach to a Java EE server running as a Windows service. If attempt to attach fails, start the application with the profiler agent instead.
Sun Java (HotSpot) client JVM can crash in attach mode due to a JVM bug
Due to a JVM bug 6776659 HotSpot client JVM can crash in attach mode.
There is no crash for the server JVM:
solves the problem.
A long pause is possible on first attempt to start CPU tracing or object allocation recording, because classes loaded before the profiler agent has attached need to be instrumented. Depending on application, it can take from several seconds to several tens of seconds, or even a few minutes in worst cases.
The good news is that there will be no pause on subsequent starts of CPU tracing or object allocation recording for the same running JVM instance.
The pause length depends on Java version. According to our experiments, the pause can be really big - 1 minute or more - when a big application gets its 10000 classes retransformed, if running on Java 6. Running same application on Java 7 dramatically reduces the pause: it becomes 3-4 times shorter.
No profiling results for some native methods.
Native methods of classes loaded before the profiler agent has attached cannot be instrumented. (There is no problem with classes loaded after the profiler has attached.)
This means that their calls will be missing in: