Java Resources, CPSC 331, Winter 2017

home page -  news -  syllabus -  schedule -  assignments -  tutorials -  java -  references -  Mike Jacobson


basics -  debugger (jdb) -  testing framework (JUnit) -  profiler (VisualVM) -  documentation (javadoc)

 Java Resources - Profiler (VisualVM)

Why is a Profiler Useful?

As mentioned during the analysis of algorithms, the kind of analysis or running times of algorithms, to be used most of the time in lecture, includes some significant simplifications. For example, we count the number of Java statements that are executed — ignoring the fact that some the time needed to execute some statements are different. A variety of factors that effect running time are ignored, as well.

Furthermore, some of the techniques that we use are only guaranteed to produce upper bounds for the worst-case running time of a program, and there these bounds are not necessarily very “tight.”

This kind of analysis is generally useful for the design stage when you are choosing an algorithm to implement (and several different algorithms, that can be used to solve the given problem, are available). It is not sufficient for software development if you are developing software that must run under significant resource restrictions; a profiler can be used to get (at least, some of) the extra information you need for this.

We will be using the profiler VisualVM in this course.

Using VisualVM

For our purposes (measuring running time of a program, we will use VisualVM in three stages:

  1. Start the VisualVM program using the command visualvm.
  2. Run the program that you are interested in.
  3. Extract your performance data from the VisualVM program window.

Using VisualVM at School

Suppose that you wish to see timing information for an execution of a program foo.class Then, first start the VisualVM program by executing the command

visualvm

As long as /usr/local/bin is in your PATH environment variable, this will bring up a VisualVM window.

Before running your program, you will need to (first time) only enable the “Startup Profiler” add-on of VisualVM by following the online instructions for “Intalling the Plugin”. This step only has to be done the first time you use visualvm.

To continue and profile your program, continue following the online instructions for “Profiling Startup”:

After the above steps, you will see a tab in the VisualVM window corresponding to the program Foo. Click on that, and your profiling results will be displayed. For each method in Foo.class, the self-time (time spent directly in the method), total time (time spent in the method and any methods called during its execution), and the number of invocations (calls) are displayed. Clicking on the various column headings sorts the output by the data in that column.

Using VisualVM at Home

If you have managed to download and install JUnit at home then you should find installing VisualVM to be reasonably straightforward. Instructions to download VisualVM and basic usage instructions, both assuming a Windows installation, are provided on the product's web page.

Comments on Optimization

Optimization is important! But it must be done selectively and with care.

Code that has been “optimized” — modified to improve efficiency — is often harder to understand and harder to maintain than the code that was optimized in order to produce it. Errors are sometimes introduced in software during optimization, too — and code that does the wrong thing, quickly, is not what we are generally trying to produce!

One of the important things that you can do with a profiler is to find out what parts of a program are being executed a lot — this is often not as obvious as you may think — so optimization can be performed, judiciously, in the places in your software where it will have the most effect.

Again, though: In general, you should optimize code only when it has been demonstrated that it is necessary to do so.

Finally, optimizing an inherently inefficient algorithm is generally not going to get you very far: If a more efficient algorithm that solves the desired problem exists, and you have implemented a (considerably) less efficient one, instead, then it is quite possible that no amount of optimization will be enough to improve your code, to the point where it is as efficient as code that would have been produced by starting with the better algorithm.

In other words, you cannot use optimization to correct a significant mistake that was made during design any more than you can use testing and debugging to do this.


Last updated:
http://www.cpsc.ucalgary.ca/~jacobs/Courses/cpsc331/W17/java/profiler.html