Search code examples
javaperformancejvmprofilerjprofiler

Difference between attaching jvm profiler using agent vs process


What is the difference between attaching a jvm profiler (in my case, its jprofiler) using a jvm agentlib and attaching dynamically to the process?

Other than advantages of static vs dynamic profiling, any other notable difference between both the approach?

Assume the profiler is running locally to the application server.


Solution

  • As Stephen said, attach mode is not available for Java 1.5 and lower (attach was introduced in Java 6) and the startup of an application can only be profiled by adding the -agentlib VM parameter. The restriction for remote profiling does not apply to JProfiler where you can use the jpenable command line tool to prepare a remote process for profiling.

    The main disadvantage for attach mode is that the profiler does not see all classes as they are loaded. Instead, for classes of interest, it has to reinstrument those classes. This takes time and before Java 8 it burdens the PermGen, so the profiler has to be more selective when instrumenting classes compared to when it's present at startup.

    For example: Recording the stack traces of array allocations is only possible if you instrument all call sites. In attach mode, this means that all classes would need to be reinstrumented which is not practical, so in JProfiler this feature is not enabled in attach mode.

    There are several other such problems which lead to feature restrictions in attach mode.