Wilson Mar bio photo

Wilson Mar

Hello!

Calendar YouTube Github

LinkedIn

Identify and resolve performance bottlenecks, memory leaks, and threading issues

US (English)   Norsk (Norwegian)   Español (Spanish)   Français (French)   Deutsch (German)   Italiano   Português   Estonian   اَلْعَرَبِيَّةُ (Egypt Arabic)   Napali   中文 (简体) Chinese (Simplified)   日本語 Japanese   한국어 Korean

US (English)   Norsk (Norwegian)   Español (Spanish)   Français (French)   Deutsch (German)   Italiano   Português   Estonian   اَلْعَرَبِيَّةُ (Egypt Arabic)   Napali   中文 (简体) Chinese (Simplified)   日本語 Japanese   한국어 Korean

Overview

This tutorial aims to enable you to identify and resolve problematic patterns in memory and CPU usage in Java and Scala code using JProfiler and JVisualVM.

Profilers in the Market

One survey published 2015-11 as pdf presented these market share numbers:

Comparison for Mac (named license with two years of support):

Product Date File Name MB $USD
JProfiler 2017-01-21 jprofiler_macos_10_0.dmg 148 MB $698
JProfiler 2016-06-21 jprofiler_macos_9_2.dmg 126 MB $698
JVisualVM 2016 GitHub 1.3.9 13 MB free
Yourkit 2016.02 yjp-2016.02-b43-mac 8.7 MB $649
jClarity Censum - - - MB $?

JVisualVM

VisualVM has the largest market share because perhaps it is free and is bundled in the JDK since JDK 6 update 7.

http://docs.oracle.com/javase/7/docs/technotes/guides/visualvm/profiler.html

The bleeding-edge version at
https://visualvm.github.io/download.html
is actively maintained by two developers in Prague, the Czech Republic.

VIDEO:

Philip Starritt has created videos on VisualVM:

  1. Launcher Setup in Eclipse. 9 July 2017.
    https://visualvm.github.io/idesupport.html

  2. Java Heap Dump Analysis of live running app. 9 July 2017
  3. OutOfMemoryError Java Heap Space Fix - Heap Dump Analysis 12 July 2017
  4. Out of Memory - Head Dump Analysis

    • https://www.youtube.com/watch?v=dUQqmnmCBbg Using jvisualvm to find Heap Problems
    • https://www.youtube.com/watch?v=pRwvx4QpUfo Java VisualVM - Introduction
    • https://www.youtube.com/watch?v=z8n7Bg7-A4I
    • https://www.youtube.com/watch?v=1kGt1ctY-a0 Introduction to Java VisualVM Java GC Monitoring with JVisualVM by Rohit Dhall

JProfiler Install & License

  1. Download from
    https://www.ej-technologies.com/download/jprofiler/files
    for your platform.

  2. PROTIP: While you wait for the download, identify the version of IDE you’ll be using with JProfiler. Most developers use integrations with their IDE IntelliJ IDEA, Eclipse, NetBeans, Oracle JDeveloper.

    Also, get a license at
    https://www.ej-technologies.com/buy/jprofiler/select

    $698 with a year of support.

    After download

  3. Open the JProfiler installer.

  4. On a Mac, drag the JProfiler icon to drop on the Applications folder.
  5. Exit the installer dialog.
  6. Delete (Move to Trash) the installer file to save disk space.
  7. Use Finder to initiate JProfiler.
  8. The first time it opens, JProfiler enters Setup. Click Next.
  9. Click Next to Evaluate for 10 days.

    IDE

  10. Optionally: Click “[Select an IDE]” if you have one and click Integrate.

    See http://resources.ej-technologies.com/jprofiler/help/doc/ide/$folder$.html

    An example of IDE configuration is http://www.andrejkoelewijn.com/blog/

  11. Click Next, Finish.

    JProfiler is started up automatically.

  12. Check “Don’t show this dialog again” and Cancel.

See Sample App Leak Memory

Just to walk through JProfiler’s UI, let’s look at a program provided by JProfiler to behave badly.

  1. Click to select the “Animated Bezier Curve demo” session which JProfile provides to intentionally leak memory not garbage collected: Its code contains a “LeakMap” object.

  2. Click Start.
  3. If an Evaluation version dialog appears, click “Evaluate”.

    The Session Starup settings dialog appears.

    Help and Docs

  4. Click the Help button for context-sensitive help.

    PROTIP: This is the best way to access specific documentation. So if you want to review documentation on another process, open the 303 page pdf JProfiler Manual at
    http://resources.ej-technologies.com/jprofiler/help/doc/help.pdf
    . It presents same content as Online Help.

  5. To continue working, click on the app dialog partially hidden by the Help dialog.

    Videos

    Among videos created by Ingo Kegel (CTO of the company) is this from 2012 for an older version of the program.

    TODO: Recreate video using new verion of JProfiler, and run Scala.

    Startup Settings

    A terminal window is opened for the demo process and the main window of JProfiler displays profiling metrics.

    PROTIP: Probes collect measurements.

  6. Click OK to accept the Startup Settings.

    A run is begun automatically.

    Start and Stop GUI

    PROTIP: In the Terminal window opened automatically, notice “JVMTI version 1.1 detected” refers to the technology the JVM provides to enable JProfiler to obtain instrumentation data.

  7. Click on Session Settings at the top menu:

    jprofiler_9 2 app settings 855x513.png

    Notice we are launching a new JVM instance and using the JVM indicated referencing the relative CLASSPATH indicated.

    Session with Attach

    Alternately, blog entry How to monitor Kafka apps with JProfiler shows this setting screen for attaching a running program:

    The app is started with this command

    java -cp `mapr classpath`:nyse/nyse-taq-streaming-1.0-jar-with-dependencies.jar -agentpath:/home/mapr/jprofiler9/bin/linux-x64/libjprofilerti.so=port=11002 com.example.Run consumer /user/iandow/mystream:mytopic
    

    Remote access

    It’s “jpenable” which loads the profiling agent and makes it possible to connect with a remote session from another computer.

    PROTIP: The profiling agent and JProfiler GUI communicate with each other through a socket. By default, the profiling agent listens on port 8849. Many networks block traffic from such a port.

    This video shows how to setup a SSH Tunnel.

  8. Click OK to dismiss the dialog.

    Watch it run

  9. Click on Telemetries category Overview.

  10. Among processes, click on the Demo to expose the app under test.

  11. Check “Leak Memory” on the BezierAnim app window.

    Programs that leak memory will show an upward trend in thread memory usage.

    PROTIP: Many memory leaks accumulate gradually over time, so may require a long run to identify conclusively.

  12. Click the zoom in and out icons at the lower right corner.

    Each GC Activity spike reduces the amount of Memory used (shown in blue) as a portion of memory allocated (shown as the top of the green block).

  13. Click on Live memory category All Objects view to sort objects by the amount each uses memory.

    Diffs in memory usage

    PROTIP: Most items are generic objects such as “java.lang.Long” which uses a lot of memory because many objects use it. We want to see the objects creating additional memory usage over time.

  14. Click the Mark Current button at the right of the top ribbon to set the baseline for differences to be displayed.

    jprofiler_9 2 ribbon-1992x180-66kb

  15. Righ-Click on the high-level object defined by a custom name (“java.awt.geom.GeneralPath”) to select “Show selection in Heap Walker”.

    “Take Heap Snapshot for Selection”.

  16. Click OK to the Heap Snapshot Options with “Select recorded objects”.

  17. Click “References” to activate the reference graph view.

    jprofiler_9.2 heap walker menu 1836x100-29kb.png

  18. Select “Cumulated outgoing references”.
  19. Click the gray arrows to expand items at each level.

    jprofiler_9 2 references-94kb

  20. Right-click on object to select “Show paths to GC Root”.

  21. Click OK to see Single Root.

  22. Scroll horizontally to the left in the chain.

    Biggest Objects

  23. Click on “Biggest Objects” icon.

  24. Expand the tree by clicking gray arrows under the app “bezier.BezierAdmin”

    jprofiler_9 2 biggest objects-101kb

    Show source code

  25. Right-click to select “Use Retain Items”.
  26. Check the “Classes” radio button, then OK.

    jprofiler_9 2 leakmap-351x68-14kb.jpg

    BLAH: Where is this in v9.2?

  27. Check the “Allocations” radio button.

    QUESTION: How do we know leakMap is within this class?

  28. Select “Cumulated allocation tree”.
  29. Click OK.
  30. Right-click to select “Show Source”.

    Save Snapshot

    PROTIP: Snapshots can be taken only while the program is running.

  31. Click the Take Snapshot camera icon on the ribbon.

    Alternately,
    click the HPROF heap snapshot.

  32. Click Save Snapshot.

    Offline

    Use the integration wizard to activate this by appending to the -agentpath VM parameter

    offline,config=[config file],id=[id]
    

Root Causes

Memory leaks was a small part of all issues according to this report in 2015

JProfiler Origin Tracker

Although Chrome announced it is doing away with Extensions, JProfiler Origin Tracker Chrome Browser Extension can still be useful.

Strategy

JProfiler has several “modes” it can run.

PROTIP: Take a “top-down” approach to analyzing a Java program.

  1. Ideally, you can attach JProfiler to a running server.

  2. It can be time consuming to create load scripts that provide the coverage of code needed. So it is often useful to see running production servers.

  3. Begin using JProfiler by using the “Profile at startup” mode.

  4. Understand a programs’s major dependencies.

  5. Identify the biggest objects.

  6. Identify the rate of memory use.

  7. Instrument classes by having JProfiler retransform the program.

  8. Run JProfile in offline mode to save snapshots when you need to analyze several JVMs running at the same time. Then have an Ant program task to compare the snapshots.

The difference run-time data areas are illustrated here: jvm architecture-650 From Java8 virtual machine architecture by Ranjith ramachandran

  • To manage the Method Area holding classes defined in source code,
    use Memory Profiling

NOTE: In Java8, metaspace setting takes the place of the MaxPermSize parameter that specifies space of the method area holding classes.

  • To manage the “thrashing” of PC (Program Counters) pointing to the next instruction to be executed in each thread,
    use CPU profiling.

  • To manage the Java Stacks containing stack frames for each chain of methods in each thread “Stack Overflow” errors refer to this area,
    use Thread Profiling.

  • Native Method Stacks per thread

  • To manage the Heap holding objects instantiated for use in JVM (garbage collected),
    use the Heap Walker.

ReTransform to Instrument Code

Other Sample Programs

http://www.javamonamour.org/2014/01/how-to-demo-garbage-collection-jconsole.html discussed in https://www.youtube.com/watch?v=Bjv_9pKiALQ

JProfiler Social Media

The company behind JProfile, ej-technologies, is based in Munich, Germany.

There has been no activity on the @JProfiler Twitter account.

There is no JProfiler group on LinkedIn, Facebook, etc.

JMC (Java Mission Control)

Java Mission Control monitors the JVM in real-time.

Java Mission Control requires a commercial license for use in production.

JMC has an Experimental Plug-in for Eclipse.

PROTIP: JMC comes bundled with the HotSpot JVM since Oracle JDK 7 Update 40 (7u40). So in a Terminal:

  1. Use the abbreviation for Java Mission Control to load it:

    jmc

    A welcome screen titled “Mission Control 5.5” appears.

    Java Mission Control User’s Guide 5.5

  2. Optional: Click the “A” at the upper-right corner to change font size.

  3. Click the program icon

    A JMX console appears.

  4. Open another terminal window to bring up the app under observation:

    java -XX:+UnlockCommercialFeatures -XX:+FlightRecorder LoadAndDeadlock
    

    NOTE: Although Oracle has built in the recording engine into the JVM run-time, it needs to be enabled in JVM start-up switches.

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

Oracle also provides optional tools for heap dump analysis and DTrace recording.

Java SE Suite provides Deterministic GC.

YourKit

YourKit from Germany has profilers for both JVM and .NET.

  1. Download from https://www.yourkit.com/java/profiler/download/

  2. In https://www.yourkit.com/purchase/ license is $649 with a year of support.

Censum

Censum from JClarity.com (by Martijn Verburg based in London) is available as both a local VM log reader (for $749/year) and as a SaaS web app.

https://blog.codecentric.de/en/2017/09/jvm-fire-using-flame-graphs-analyse-performance/

CTO/CEO Martijn Verburg (@karianna) speaks at VIDEO: The Diabolical Developer’s Guide to Performance Tuning introduces the Performance Diagnostic Methodology (PDM).

Tapiki

Takipi is focused on Scala performance tuning. It is the only tool I know of that shows numbers associated with Scala code. It’s free up to 2 servers for Scala.

What to Look For

https://shipilev.net/blog/2016/close-encounters-of-jmm-kind/

As a comparison, take a look at the profiler in Visual Studio for C# Xamarin code.

Wait, there’s more. Click one of these …

This article is one of a series about tuning and performance: