Can you run java hprof and a custom agentlib together?

We run newrelic, which is an Advanced Performance Management (APM) software stack that will profile your java application from the browser all the way to the database. It is really good software, affordable, and highly recommended.

I also wanted to add the hprof JVM profiling utility to our runtime configuration, but didn’t know if this was even possible. This is just a quick note that shows it is. Using our previous javassist byte code injection example, we can see that we can also the load the hprof agent.

atllpecomodb01:oracle:atlecomp1:/u01/orahome>java -agentlib:hprof=cpu=samples -javaagent:myAgent.jar foo
started method at Sun Apr 28 07:57:26 EDT 2013
ran doIt() method
ended method at Sun Apr 28 07:57:32 EDT 2013
Dumping CPU usage by sampling running threads ... done.
atllpecomodb01:oracle:atlecomp1:/u01/orahome>awk '{if ($0 ~ "CPU SAMPLES BEGIN") {l=1;print} else if (l == 1) {print}}' java.hprof.txt
CPU SAMPLES BEGIN (total = 19) Sun Apr 28 07:57:32 2013
rank   self  accum   count trace method
   1 10.53% 10.53%       2 300145 java.lang.System.arraycopy
   2 10.53% 21.05%       2 300167 java.lang.System.arraycopy
   3  5.26% 26.32%       1 300001 java.lang.ClassLoader.findBootstrapClass
   4  5.26% 31.58%       1 300023 java.io.UnixFileSystem.canonicalize0
   5  5.26% 36.84%       1 300072 javassist.bytecode.ExceptionTable.
   6  5.26% 42.11%       1 300086 javassist.compiler.MemberResolver.
   7  5.26% 47.37%       1 300108 java.io.UnixFileSystem.getBooleanAttributes0
   8  5.26% 52.63%       1 300131 sun.misc.URLClassPath$JarLoader.getClassPath
   9  5.26% 57.89%       1 300139 java.io.BufferedInputStream.read
  10  5.26% 63.16%       1 300144 javassist.bytecode.AttributeInfo.
  11  5.26% 68.42%       1 300161 javassist.bytecode.stackmap.TypeData.join
  12  5.26% 73.68%       1 300170 javassist.bytecode.LongVector.addElement
  13  5.26% 78.95%       1 300187 javassist.bytecode.Utf8Info.write
  14  5.26% 84.21%       1 300196 java.lang.System.arraycopy
  15  5.26% 89.47%       1 300203 sun.util.LocaleServiceProviderPool.
  16  5.26% 94.74%       1 300215 java.util.LinkedHashMap.createEntry
  17  5.26% 100.00%       1 300216 java.util.ArrayList.
CPU SAMPLES END
atllpecomodb01:oracle:atlecomp1:/u01/orahome>

I realize this is overkill, as newrelic will profile this as well, but it has to be requested through the newrelic interface, and only for a period of time. Loading the hprof agent will record the profile over the life of the JVM.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.