Profiling is an excellent way to find resource bottlenecks in a program. Java has built-in profiling tools that you can use to easily view a profile of a program’s runtime execution.
To generate a profiling report for a Java program, add the hprof profiler to the program’s command line as follows.
java -Xrunhprof:cpu=samples,depth=30,file=myprogram.hprof other arguments...
When the program terminates, it writes a profile report to myprogram.hprof. JDC Tech Tips has an article on Using HPROF to Tune Performance, which explains the format of the profile file. But you’ll probably want to skip straight ahead to using a GUI program to view the file, so…
There is a free tool with the unfortunate name of PerfAnal that analyses profile files and presents them using a graphical interface. PerfAnal: A Performance Analysis Tool on Sun’s Java site contains instructions and a link to download the tool. A few minutes’s tinkering with this can yield valuable insights into the workings of a program.