Profiling in Java

Task assignment

Your task is to speed up Genetic algorithm (GA) solving Travelling Salesman Problem (TSP).

You probably need to do the following steps to achieve the desired speedup:

  1. Download program from git repository: git clone
  2. Open the source code in IDE
  3. Run the program (cz.esw.profiling.Main class)
  4. Do profiling (hints below)
  5. Make changes which will improve the speed (code modifications)
  6. Upload patch file into upload system and pass specified limit: Patch will be applied using git apply command, therefore, best way to generate patch is the git diff command:
    git fetch origin && git diff origin/master > tsp.diff.txt
    (txt file type because of upload system).

You are not allowed to modify the parameters of the GA and to parallelize the algorithm! The possible speed up (with the preset parameters) is great (approx. from 120s to 4s).

Profiling tools

The JDK itself provides a number of monitoring tools located in 'JAVA_HOME/bin' folder.

Java VisualVM

The Java VisualVM is a baseline for profiling Java applications. It allows you to use both sampling and instrumentation approaches to profile the application. It allows to measure time spent in functions and see statistics about objects on the heap. It can also generate and analyse heap dumps, monitor garbage collector (and invoke it) and monitor threads.

Java Mission Control with Java Flight Recorder

Unlike the rest, Java Mission Control is a commercial feature (free for development) of Oracle JDK. It can be used for monitoring provided also by VisualVM but its main feature is the Java Flight Recorder (JFR). The JFR can record detailed information about various aspects of the application - e.g. object statistics, compilation, detailed GC info, I/O operations, exceptions,etc.


JConsole is a tool with a basic monitoring functionality - heap size, CPU usage, thread monitor, etc. But it also allows using JMX instrumentation in order to see JVM parameters and in some cases it also allows you to change the parameters.

There are also some command line tools (jcmd, jhat, jinfo,…) but most of their functionality is covered by the tools described above.


Try to find the code that is executed a lot and find out if the results cannot be precomputed and reused. Avoid unnecessary object creation e.g. boxing and unboxing of primitive variables (int vs. Integer).

courses/b4m36esw/labs/lab02.txt · Last modified: 2018/02/28 20:44 by cuchymar