JIP — The Java Interactive Profiler |
||
hprof
tool
that ships with the JDK. There are, however, a few differences:
hprof
is not an interactive
profiler. It starts when your program starts and ends when the
JVM exits. In many cases this doesn't give you a true
measure of performance since the Just In Time compiler doesn't
compile code on the first pass. In addition, this type of profiler
is not useable at all in web applications since you end up
profiling the web container as well as the web application.
JIP, on the other hand, allows you to turn the profiler
on and off while the JVM is running.JVMPI
(Java Virtual Machine Profiling Interface)
which requires the use of native components. JIP, however,
is pure Java. It takes advantage of the Java5 feature
which allows you to hook the classloader. JIP adds
aspects to every method of every class that you want
to profile. These aspects allow it to capture performance
data.hprof
will cause a program to run 20 times slower. JIP, on the other
hand, is lightweight. A VM with profiling turned on is about
twice as slow as one without a profiler. When the profiler
is turned off, there is almost no overhead associated with
using JIP.hprof
, for
example, will show you the relative amount of time that is
spent in different parts of your code, but hprof
has so much overhead, that you cannot use it to get real
world timing measurements. JIP, on the other hand, actually
tracks the amount of time used to gather performance data
and factors that time out of its analysis. This allows you
to get close to real world timings for every class in your
code. So there is no need to litter your code with
System.currentTimeMillis()
!To run the profiler, you need the following:
profile.jar
These files are loaded by the application classloader and should not be in the extentions loader path. The jar files need to be in the same directory. The properties file can be anywhere.
To use the profiler, you need to use the following JVM arguments:
-javaagent:[DIR]\profile.jar -Dprofile.properties=[DIR2]\profile.properties
where [DIR] is the directory that contains the profile.jar
Note: Due to a bug in the JDK on OS X, [DIR] must be a fully qualifed path.and [DIR2] is the directory that contains the profile.properties
By default (if you don't give a -Dprofile.properties
),
profiling starts out turned on and the remote
interface (use to profile interactively) is turned off (see
the profile.properies
for more information). In
this case JIP works just like hprof
, although it is
much faster.
When using with stock Tomcat, set the java agent by using the
env. variable JAVA_OPTS
. For example, on Windows
use the following:
SET JAVA_OPTS=-javaagent:[DIR]\profile.jar -Dprofile.properties=[DIR2]\profile.properties
Where [DIR1] and [DIR2] are described as above.
In some cases, like webapps, you probably want to start out with the profiler turned off. When you get to some case you want to test, you'd like to turn it on, run the test case, turn the profiler off and dump the results. To do these things, you first of all need to change the profile properties file:
profiler=off
remote=on
port=15599
To interact with the profiler you need the client.jar
which is
in the /client
directory of the JIP distribution.
File.bat localhost 15599 c:\tmp\test-profile.txt
Start.bat localhost 15599
Finish.bat localhost 15599
example.bat
,
example-ant-1.5.sh
and example.sh
. All use the
example of compiling JIP with ant. example.bat
and
example-ant-1.5.sh
work only with ant 1.5 and example.sh
works only with ant 1.6 (this is because the way that ant gets invoked
changed between the two releases).