Volitale keyword and JMM memory model. But the theoretical things are really easy to forget, see and touch. As a result, I searched the Internet to see the implementation of the underlying machine instructions and found a number of articles about seeing Java compiled assembly code, which led me to the jitWatch tool, whose name also suggests jit compiler monitoring.

I am very grateful for my curiosity, which made me determined to learn assembly language by myself for a year. Because I am a junior college student, the school does not offer assembly language, and my first programming language is ALSO C. After LEARNING C, I was not satisfied with the principle of code operation and started assembly by myself. Have seen the assembly of small turtle teaching video, the first book is Wang Shuang’s 8086 assembly, followed by 386 and Win32 assembly.

With little recognition of assembly, let me interested in Java bytecode, so when learning the JVM last year, found that Java bytecode is quite easy, and later because of interest, to know the asm the kit can be used to write or rewrite class at runtime bytecode, online can’t find any document, just bite the bullet and go to see the API documentation, Google Translate. So after a bunch of trial and error, I’m finally using ASM. It seems that I have two small frameworks on Github which are implemented with ASM writing section code. The most recent one is to implement asynchronous methods, and I have also written dynamic proxy and call chain monitoring based on bytecode implementation.

But because of my limited ability, I only learned the surface. I think I can understand a little bit more. Today I want to share with you how to view jIT-compiled assembly code in Java.

Java files are compiled to generate class files that contain bytecode, which is interpreted and executed by the JVM. To improve program efficiency, Java provides a JIT compiler that compiles hot code. If a piece of code is called often, it will be jit compiled into machine code and subsequent calls will no longer explain execution. So to see assembly code you have to have that code called multiple times.

Use HSDIS to view assembly code

HSDIS is short for HotSpot Disassembler, to use HSDIS you need to download HSDIS-amd64.dylib.

Hsdis-amd64. dylib for MAC

https://github.com/evolvedmicrobe/benchmarks/blob/master/hsdis-amd64.dylib?spm=a2c4e.10696291.0.0.13ce19a46a5XRa&file=hs dis-amd64.dylibCopy the code

Put it in the $JAVA_HOME/jre/lib/ directory

Java files can be compiled and run using the javac command from the command line. In the command line Java – XX: + PrintAssembly – XX: + UnlockDiagnosticVMOptions XXX > > assembly_code. TXT can get a lot of assembly code. You can also configure VM parameters in IDEA, as shown in the following figure.

Debug will output assembly code in the CONSOLE of IDEA

But this approach is not friendly, will be Java package code output, it is difficult to find a method of a class assembly code, if only a simple Java class fine.

Plug-ins that use VisualVM

I haven’t tried this approach, but there is a plugin called SAPlugin.

Using JITWatch

Jitwatch is an open source project with detailed usage documentation. You need to download the code and compile it yourself. Download link:

https://github.com/AdoptOpenJDK/jitwatch/releases
Copy the code

Instead of downloading code from the Master branch, download the releases version. Downloading code directly from the Master branch causes compilation problems, so I chose to download the Releases version instead. Compilation run can be seen in wiki. Gradle is compiled and run using Gradlew clean build Run.

  • Step 1: Open a code file,JITWatchsupportkotlin,Java,ScalaSuch as running inJVMIn the language
  • Step 2: Configuresandbox, see below
  • Step 3: Select the language type of the source file
  • Step 4: ClickRun. usejavacThe command compiles and runs through-cpPoint to thesandboxThe directory isclasspath, it isjavaAfter the code source file is compiledclassPath to the storage.

  • After configuration and compilationclassThe path where the files are stored is also the runtime user classpathclasspath.
  • configurationVMArgument to print assembly code

The red box in the figure shows the source code and the mapping of bytecode and assembly code. The red box can be removed by using the up and down arrow keys. In addition to code mappings, you can also choose to look at a method. In addition, you can return to the main window to see which classes were JIT compiled, and you can open TriView to view assembly code by selecting a class of interest.