Welcome to github.com/hsfxuebao/j… , hope to help you, if you feel ok, please click on the Star

5. JProfiler

Basic overview

introduce

When running Java, you sometimes want to test the memory footprint of the runtime, so you need to use the test tool to check. There is the Eclipse Memory Analyzer Tool (MAT) plug-in in Eclipse for testing, and there is a plug-in in IDEA, called JProfiler.

JProfiler is a Java application performance diagnostic tool developed by EJ-Technologies. Powerful, but charged.

Official website download address:

www.ej-technologies.com/products/jp…

The characteristics of

  • Easy to use, user-friendly interface (simple and powerful)
  • Low impact on the analyzed application (template provided)
  • CPU, Thread, Memory analysis is particularly powerful
  • Supports analysis of JDBC, noSql, JSP, servlet, socket, etc
  • Support multiple modes of analysis (offline, online)
  • Support for monitoring local and remote JVMS
  • Cross-platform, with a variety of operating system installed versions

The main function

  1. The method call

Can an analysis of method calls help you understand what your application is doing and find ways to improve its performance

Memory allocation

Analyzing objects on the heap, reference chains, and garbage collection can help you fix memory leaks and optimize memory usage

  1. Threads and locks

JProfiler provides a variety of analytical views on threads and locks to help you find multithreading problems

  1. High level subsystem

Many performance problems occur at higher semantic levels,

Installation and Configuration

Download:

www.ej-technologies.com/download/jp…

Download and Install

Configure IDEA in JProfiler

The IDEA of integration of JProfiler

The specific use

Data collection mode

JProfier data collection modes are divided into two types: Sampling and Instrumentation.

Instrumentation: This is JProfiler full function mode. Before the class is loaded, JProfier writes the relevant functionality code into the bytecode of the class to be analyzed, affecting the running JVM.

  • Advantages: Powerful. In this setting, the call stack information is accurate.

  • Disadvantages: If there are a large number of classes to be analyzed, the performance of the application is greatly affected and the CPU overhead may be high (depending on Filter control). Therefore, this pattern is generally used in conjunction with filters, and only specific classes or packages are classified

Sampling: Similar to sample statistics, the information in method stack of each thread stack shall be counted out at a certain interval (5ms).

  • Advantages: Very low CPU overhead, minimal application impact (even if you don’t configure any filters)
  • Disadvantages: Some data/features are not available (e.g., number of method calls, execution time)

Note: JProfiler itself does not specify the type of collection for the data; the collection type here is for method calls.

Because most of the core functionality of JProfiler relies on data collected by method calls, it is straightforward to think of this as JProfiler’s data collection type.

Instrumentation reconstruction mode

Sampling model

Remote sensing monitoring Telemetries

Summary:

Memory:

Object situation:

Throughput:

GC:

Class:

Thread situation:

CPU负载情况 :

Memory view Live Memory

Live Memory Memory profiling: Information about class/class instance. Such as the number of objects, size, object creation method execution stack, object creation hotspot.

  • All Objects

Displays a list of all loaded classes and the number of instances allocated on the heap. Only Java 1.5(JVMTI) shows this view.



  • Recorded Objects are Recorded

View the allocation of objects for a specific time period and record the call stack for the allocation.

  • Allocate the access Tree to the Call Tree

Displays a request tree or J2EE component with annotated assignment information for a method, class, package, or selected class.

  • Allocate Hot Spots

Displays a list of methods, classes, packages, or J2EE components that assign selected classes. You can annotate the current value and display the difference value. For each hotspot, its trace record tree can be displayed.

  • Class Tracker

The class trace view can contain any number of charts showing instances and times of selected classes and packages.

Analysis: The condition of objects in memory

  • Frequently created Java objects: infinite loops, too many loops
  • Large objects exist: Byte [] should read and write files. Byte [] is too large if not written for a long time
  • Memory leak

The heap walker

CPU view cpuviews

JProfiler provides different ways to log access trees to optimize performance and detail. Threads or thread groups and thread states can be selected by all views. All views can be aggregated into different layers such as methods, classes, packages, or J2EE components.

Access the Call Tree

Displays an accumulated top-down tree containing all access queues recorded in the JVM. JDBC,JMS, and JNDI service requests are annotated in the request tree. The request tree can be split based on the different URL needs of servlets and JSPS.

Hot Hot Spots

Displays a list of methods that consume the most time. The traceback tree can be displayed for each hotspot. The hotspot can be evaluated by method requests, JDBC,JMS, and JNDI service requests, and by URL requests.

Visit the Call Graph

Displays a graph of the access queue starting from the selected method, class, package, or]2EE component.

Method Statistis for Method Statistis

Displays the call time details of recorded methods over time.

Threads view

JProfiler monitors thread history to determine its health, monitors for thread blocking, and presents methods managed by a thread in a tree form. Profiling threads.

Thread History Thread History

Displays an activity schedule with thread activity and thread state.

Thread Monitor

Displays a list of all active threads and their current active status.

Thread Dumps Thread Dumps

Displays stack traces for all threads.

Thread analysis is mainly concerned with three aspects:

  1. Maximum number of threads for a Web container. For example, the Tomcat thread capacity should be slightly larger than the maximum number of concurrent requests.
  2. Thread block
  3. The thread deadlock

Monitor & Lock Monitor &locks

Monitors and Locks Monitors & Locks The holding of Locks by all threads and lock information

Observe the JVM’s internal threads and view the status:

The Current Locking Graph

Displays the current deadlock chart in the JVM

Monitor Current Monitors

Displays the monitors currently in use and includes their associated threads

Locking History Graph

Displays the locking history recorded in the JVM

History Monitor History

Displays a history of significant waiting events and blocking events

The Monitor uses the Monitor Usage Statistics

Displays statistical monitoring data for group monitoring, thread monitoring, and monitoring classes

Case analysis

Case 1

package com.cpucode.java.gui.tools.jprofiler; import java.util.ArrayList; import java.util.concurrent.TimeUnit; Public class Jprofiler {public static void main(String[] args) {while(true){ArrayList list = new ArrayList(); int num = 500; for (int i = 0; i < num; i++) { Data data = new Data(); list.add(data); } try{ TimeUnit.MILLISECONDS.sleep(500); }catch (InterruptedException e){ e.printStackTrace(); } } } } class Data{ private int size = 10; private byte[] buffer = new byte[1024 * 1024]; private String info = "cpuCode"; }Copy the code

Case 2

package com.cpucode.java.gui.tools.jprofiler;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

/**

 */
public class MemoryLeak {
    public static void main(String[] args) {
        while (true){
            ArrayList beanlist = new ArrayList();
            int num = 500;

            for (int i = 0; i < num; i++) {
                Bean data = new Bean();

                data.list.add(new byte[1024 * 10]);
                beanlist.add(data);
            }

            try{
                TimeUnit.MILLISECONDS.sleep(500);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        }
    }
}

class Bean{
    int size = 109;
    String info = "cpuCode";
    static ArrayList list = new ArrayList();

    //ArrayList list = new ArrayList();
}
Copy the code

6. Arthas

Basic overview

background

Earlier, we introduced free tools such as JVisualVM, which comes with the JDK, as well as commercial tools such as Jprofiler.

These two tools are well known in the industry. The advantage of these two tools is that you can view performance data of various dimensions on the GRAPHICAL interface. Users can analyze the data comprehensively and determine where performance problems occur.

However, both tools have the disadvantage of having to configure monitoring parameters in the server project process. The tool then connects to the project process remotely to retrieve the relevant data. This can cause some inconvenience, for example, the network of the online environment is isolated, and the local monitoring tool cannot connect to the online environment. And commercial tools, like Jprofiler, are paid for.

Is there a tool that doesn’t require a remote connection, doesn’t require monitoring parameters, and still provides rich performance monitoring data

Arthas is an open source performance analysis tool from Alibaba.

An overview of the

Arthas is an open source Java diagnostics tool on Alibaba that developers love. Troubleshoot faults online without restart. Dynamically trace Java code; Monitor JVM status in real time.

Arthas supports JDK6+, Linux/Mac/Windows, command line interaction, and Tab auto-completion to further locate and diagnose problems.

Arthas can help you when you are stuck with a problem like the following:

  • From which JAR is this class loaded? Why are all kinds of class-related exceptions reported?
  • Why didn’t the code I changed execute? Did I not commit? Got the branch wrong?
  • If you encounter a problem, you cannot debug it online. Can you only re-publish it by logging?
  • There is a problem with a user’s data processing online, but it cannot be debugged online, and it cannot be reproduced offline!
  • Is there a global view of the health of the system?
  • Is there any way to monitor the real-time health of the JVM?
  • How to quickly locate application hot spots, generate flame map?

Based on what tools

  • Greys – Anatomy :Arthas code is based on the second development of Greys, thank you very much for all the previous work of Greys and the comments and suggestions of the original greys authors to Arthas!

  • Termd :Arthas command line implementation based on TerMD is an excellent command line development framework thanks to TerMD.

  • Crash :Arthas’s text rendering feature is based on the text rendering feature in Crash, the source code can be seen here, thanks to Crash for doing a great job on this.

  • Cli :Arthas’s command line interface is based on the CLI library provided by vert.x, thanks to vert.x for a good job in this area.

  • Compiler Arthas the source of the memory compiler code

  • Telnet Client code source in Apache Commons Net Arthas

  • JavaAgent: Interceptor that runs before main and has a built-in method named premain, meaning that the premain method is executed first and then main

  • ASM: A general-purpose Java bytecode manipulation and analysis framework. It can be used to modify existing classes or to generate classes dynamically directly in binary form. ASM provides common bytecode conversion and analysis algorithms from which you can build custom complex conversion and code analysis tools. ASM provides similar functionality to other Java bytecode frameworks, but focuses on performance. Because it is designed and implemented as small and fast as possible, it is ideal for use in dynamic systems (and of course statically, for example, in compilers)

Official usage document

arthas.aliyun.com/zh-cn/

Installation and use

The installation

Installation Method 1: You can run commands to download the file on Linux

You can download it on Github, or if it is slow, you can try Gitee.

  • Making the download

    Wget alibaba. Making. IO/arthas/arth…

  • Gitee download

    Wget arthas. Gitee. IO/arthas – the boot…

Installation method 2: can directly in the browser to visit alibaba. Making. IO/arthas/arth… After the download succeeds, upload the file to the Linux server.

Uninstall:

Delete the following files on Linux/Unix/Mac:

rm -rf ~/.arthas/

rm -rf ~/logs/arthas
Copy the code

Windows platform directly delete the. Arthas and logs/arthas directories under user Home

Project directory

  • Arthas-agent: Agent based on JavaAgent technology

  • Bin: Some startup scripts

  • Arthas-boot: Java version of the one-click install startup script

  • Arthas-client: Telnet client code

  • Arthas-common: Some common utility classes and enumeration classes

  • Arthas-core: Core library for interaction and implementation of various arthas commands

  • Arthas-demo: Sample code

  • Arthas-memorycompiler: memorycompiler code, Fork from github.com/skalogs/Ska…

  • Arthas-packaging: Maven packaging related

  • Arthas-site: Arthas site

  • Arthas-spy: Facets woven into the target class

  • Static: indicates static resources

  • Arthas – testcase: test

Start the

Arthas is just a Java program, so you can run it directly from Java-JAR.

Upon successful execution, Arthas provides a command line interaction where arthas detects Java processes on the current server and displays a list of processes for the user to enter a number (1, 2, 3, 4…). Make a selection and press Enter.

Method 1:

java -jar arthas-boot.jar
Copy the code

# Select process (enter [] id (not PID) press Enter)

[INFO] arthas-boot version:3.1.4 [INFO] Found Existing Java process, please choose one and hit RETURN. * [1]:11616 com.Arthas [2]:8676 [3]:16200 org.jetbraihs.jps.cmdline.Launcher [4]:21032  org.jetbrains.idea.maven.server.RemoteMavenServerCopy the code

Method 2: Select a Java process PID during runtime

java -jar arthas-boot.jar [PID]
Copy the code

Check the process

See the log

cat ~/logs/arthas/arthas.log
Copy the code

See the help

java -jar arthas-boot.jar -h
Copy the code

web console

In addition to viewing from the command line, Arthas currently supports the Web Console. After successfully starting the connection process, it is automatically started and can be accessed directly by visiting http://127.0.0.1:8563/. The operation mode on the page is exactly the same as the console.

exit

The last line [arthas@7457]$indicates that the monitoring client is opened, and relevant commands can be executed here to view it.

Exit the current client

quit

exit
Copy the code

Close the arthas server and exit all clients

stop

shutdown
Copy the code

Correlation diagnostic instruction

Basic instructions

  • Help — View the command help information
  • Cat — Prints the contents of a file, similar to the cat command in Linux
  • Echo – Prints parameters, similar to the echo command in Linux
  • Grep — Match lookup, similar to the grep command in Linux
  • Base64 – Base64 encoding conversion, similar to the Base64 command in Linux
  • Tee — copies standard input to standard output and the specified file, similar to the tee command in Linux
  • PWD — Returns the current working directory, similar to the Linux command
  • CLS — Clears the current screen area
  • Session — View information about the current session
  • Reset — Resets enhanced classes. This will restore all Arthas enhanced classes and reset all enhanced classes when Arthas server is closed
  • Version – Prints the Arthas version number loaded by the current target Java process
  • History – Prints command history
  • Quit — Exit the current Arthas client, leaving other Arthas clients unaffected
  • Stop — Close the Arthas server and exit all Arthas clients
  • Keymap — Arthas list of shortcuts and custom shortcuts

The JVM related

  • Dashboard – Real-time data panel for the current system
  • Thread – Views the thread stack information for the current JVM
  • JVM – View information about the current JVM
  • Sysprop – View and modify system properties of the JVM
  • Sysenv – View environment variables for the JVM
  • Vmoptions – View and modify options for diagnostics in the JVM
  • Perfcounter – View Perf Counter information for the current JVM
  • Logger – View and modify logger
  • Getstatic — View the static properties of the class
  • Ognl — Executes ogNL expressions
  • Mbean – View information about mbeans
  • Heapdump — Dumps Java heap, similar to the heapdump function of the jmap command

The class/this correlation

  • Sc – View information about classes loaded by the JVM
  • Sm — View method information for loaded classes
  • Jad – Decompiles the source code of a loaded class
  • MC — memory compiler, memory compiles.java files as.class files
  • Retransform — Load an external. Class file and retransform it into the JVM
  • Re-define — Load an external. Class file, re-define it into the JVM
  • Dump — Dumps loaded class byte code to a specific directory
  • Classloader – view the classloader inheritance tree, urls, class loading information, and use classloader to getResource

Monitor/watch/trace

Please note that these commands, through the bytecode enhancement technology to realize, will insert some aspects in the specified class methods to implement the data statistics and observation, so online, use pretest, please try to clear need classes, methods and conditions for observation, diagnosis end to stop or will enhance the reset command.

  • Monitor — method performs monitoring
  • Watch – method performs data observations
  • Trace — calls to the method internally and prints the time spent on each node on the method path
  • Stack – Outputs the call path where the current method is called
  • Tt – time tunnel of method execution data, recording input and return information for each call to the specified method, and observing these different time calls

other

Profiler/flame figure

options

7. Java Mission Contr

history

Prior to Oracle’s acquisition of Sun, Oracle’s JRockit Virtual Machine provided a virtual machine diagnostic tool called JRockit Mission Control.

After Oracle acquired Sun, Oracle owned both Sun Hotspot and JRockit virtual machines. In line with Oracle’s strategy for Java, the best features of JRockit will be ported to Hotspot in the future. One significant improvement is the addition of JRockit support to Sun’s JDK.

Mission Control is a tool that has been bundled with the Oracle JDK since Oracle JDK 7U40.

The JFR introduced in this section has been open source since Java 11. But in previous Java versions, JFR was a CommercialFeature that needed to be enabled with the Java virtual machine parameter -xx :+UnlockCommercialFeatures

If you’re interested, check out the OpenJDK’s Mission Control project.

Github.com/JDKMissionC…

Start the

Mission Control is located at %JAVA_HOME%/bin/jmc.exe. Open this software.

An overview of the

Java Mission Control (JMC) is a powerful tool provided by Java. Is a suite of tools for managing, monitoring, profiling, and troubleshooting Java applications.

It includes a GUI client and a number of plug-ins to collect Java virtual machine performance data, Examples include the JMX Console, which provides access to MXBeans, which is used to store the running data of each subsystem of the virtual machine, and Java Flight Recorder (JFR), an efficient profiling tool built into the virtual machine.

Another advantage of JMC is that sampling, rather than traditional code placement techniques, has a very, very small impact on application performance and can be done with JMC on (the only impact may be fullGC).

Function: Monitors JVM runtime status in real time

If you are using a remote server, turn on JMX before using it.

  • Dcom.sun.management.jmxremote.port = $ { YOUR PORT }
  • Dcom.sun.management.jmxremote
  • Dcom.sun.management.jmxremote.authenticate=false
  • Dcom.sun.management.jmxremote.ssl=false
  • Djava.rmi.server.hostname=${YOUR HOST/IP}

File -> Connect -> Create a new connection and fill in the host and port for the above JMX parameters

Java Flight Recorder

Java Flight Recorder is one of the components of JMC.

The Java Flight Recorder can collect performance data from Java virtual machines with very low performance overhead.

JFR’s performance overhead is small, averaging less than 1% in the default configuration. Compared to other tools, JFR provides direct access to data within a virtual machine without affecting the optimization of the virtual machine. As such, it is ideal for Java programs running at full capacity in production environments.

Java Flight Recorder and JDK Mission Control work together to create a complete tool chain. JDK Mission Control enables efficient, detailed analysis of the Java Flight Recorder’s continuous collection of low-level and detailed runtime information.

The event type

When enabled, JFR logs a series of events that occur during the run. These include Java level events, such as thread events, lock events, and events within the Java Virtual machine, such as new object creation, garbage collection, and just-in-time compilation events.

According to the timing and duration, there are four types of JFR events, which are as follows.

  1. Instant events, such as exceptions and thread start events, are what users care about.
  2. Duration events, where users care about their Duration, such as garbage collection events.
  3. A Timed Event is a continuous Event that exceeds a specified threshold.
  4. Sample events are periodic sampling events.

One common example of Sampling events is Method Sampling, which counts stack traces of individual threads at intervals. If there is a recurring method in these sampled stack traces, we can assume that it is a hot method.

Start the way

Mode 1: Use-XX:StartFlightRecording=parameter

The first is to add the -xx :StartFlightRecording= parameter when running the target Java program.

For example, in the following command, JFR collects data after the Java VM starts for 5s (delay=5s) and lasts for 20s(duration=20s). JFR saves the collected data to a specified file (filename= myrecord.jFR)

java -XX:StartFlightRecording=delay=5s,duration=20s,filename=myrecording.jfr,settings=profile MyApp
Copy the code

Since JFR will continue to collect data, if left unchecked, JFR can fill up all the space on your hard drive. Therefore, it is necessary to limit the data collected in this mode.

Such as:

java -XX:StartFlightRecording=maxage=10m,maxsize=100m,name=SomeLabel MyApp
Copy the code

Approach 2: Use JFR of JCMD.

Sons command

JCMD allows JFR to start collecting data, stop collecting data, or save the collected data using the subcommands jfr. start, jfr. stop, and jfr.dump.

$ jcmd <PID> JFR.start settings=profile maxage=10m maxsize=150m name=SomeLabel
Copy the code

After the above command is run, the JFR in the target process has started collecting data. At this point, we can export the collected data by using the following command:

$ jcmd <PID> JFR. dump name=SomeLabel filename=myrecording.jfr
Copy the code

Finally, we can close the JFR in the target process with the following command:

  $ jcmd <PID> JFR.stop name=SomeLabel
Copy the code

Mode 3: JFR plug-in for JMC

Java Flight Recorder sampling analysis

To use sampling, you must first add parameters:

-XX:+UnlockCommercialFeatures
-XX:+FlightRecorder
Copy the code

Otherwise:

The sampling time is 1 minute by default and can be adjusted as needed. Event setting is selected as profiling. You can then set sampling profile information such as:

  • Java Virtual Machine-> Detailed->Object Count/Object Count after GC

  • Java Virtual Machine -> Method Profiling Sample/Method Profiling Sample Information

  • Java Application -> File Read/Filewrite/Socket Read/Socket Write

The Profile is then started, and at the end of time the Profile will automatically download the record back and display it in the JMC.

From the display information, we can read about memory and CPU information, code, threads, IO and other important information display.

code

IO

8. Other tools

Flame Graphs

In the pursuit of extreme performance, it is important to know what the CPU is doing during the execution of your application, and the flame chart is a very intuitive tool to show how the CPU allocates time during the entire application life cycle.

The flame chart should be familiar to modern programmers, as it is a tool that visually shows the CPU consumption bottleneck in the call stack.

Much of the discussion about Java fire charts on the web comes from Brendan Gregg’s blog:

www.brendangregg.com/flamegraphs…

Flame chart, which simply measures time by the width of the x axis, and the y axis represents the level of thread stack.

Tprofiler

Case study:

JVM tuning using tools provided by the JDK itself can increase TPS from 2.5 to 20(a seven-fold increase) and pinpoint system bottlenecks.

System bottlenecks include: there are not too many static objects in the application, a large number of business threads frequently create temporary objects with a long life cycle, and problems in the code.

So, how to accurately locate the performance code in the mass of business code? TProfiler, alibaba’s open source tool, was used to locate the performance code, which successfully solved the performance bottleneck of too frequent GC, and ultimately improved TPS by a factor of 4 to 100 over the last optimization.

  • TProfiler configuration, deployment, remote operation, and log reading are all very simple. However, it can be used to hit the nail on the head and help us solve the performance bottleneck of GC too frequently.

  • The most important feature of TProfiler is the ability to count the top methods for your JVM over a given period of time, and these top methods are likely to be the cause of your JVM’s performance bottlenecks. This is something that most other JVM tuning tools don’t have, including JRockit Mission Control. JRokit lead developer Marcus Hirt, in a comment on his personal blog Low Overhead Method Profiling with Java Mission Control, made it clear that JRMC does not support the TOP Method.

  • TProfiler download:

Github.com/alibaba/TPr…

Btrace

Java runtime tracing tool

Common dynamic tracking tools are BTrace, HouseMD(the project is no longer in development), Greys-Anatomy, and Byteman(JBoss). Note that Java runtime tracking tools are not limited to these, but they are relatively common.

BTrace is an open source project based on SUN Kenai cloud computing development platform to provide secure and reliable dynamic trace analysis tools for Java. Let’s look at the official definition of BTrace:

BTrace is a safe, dynamic tracing tool for the Java platform. BTrace can be used to dynamically trace a running Java program (similar to DTrace for OpenSolaris applications and OS). BTrace dynamically instruments the classes of the target application to Inject tracing code (” Bytecode tracing “).

A secure dynamic tracking tool for the Java platform. Can be used to dynamically trace a running Java program.

BTrace dynamically adjusts the classes of the target application to inject trace code (” bytecode tracing “).

YourKit

JProbe

Spring Insight

Reference: blog.csdn.net/qq\_4422609…