Warning
This page is located in archive. Go to the latest version of this course pages.

Profiling in Java

Task assignment

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

Brief recap of TSP and GA can be seen in PDF

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

  1. Download program from git repository: git clone https://gitlab.fel.cvut.cz/cuchymar/tsp.git
  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

Oracle JDK 8 itself provides a number of monitoring tools located in 'JAVA_HOME/bin' folder. In later releases, some tools are not included and have to be installed individually.

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.

https://visualvm.github.io/documentation.html

http://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/tooldescr010.html

Java Mission Control with Java Flight Recorder

Although, JMC is now open source, we recommend you to use Oracle JDK 8 with bundled JMC. To use it on machines in the lab, change project JDK to /opt/jdk1.8.0_162

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.

Flight Recording can be started automatically with VM arguments:

  -XX:+UnlockCommercialFeatures -XX:+FlightRecorder -XX:+UnlockDiagnosticVMOptions -XX:+DebugNonSafepoints -XX:StartFlightRecording=duration=10s,filename=myrecording.jfr 

https://docs.oracle.com/javacomponents/index.html

http://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/tooldescr003.html

https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/tooldescr004.html

http://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/tooldescr005.html

JConsole

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.

http://docs.oracle.com/javase/8/docs/technotes/guides/management/jconsole.html

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

Tips

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: 2019/02/25 19:07 by cuchymar