An overview of the

When you say THE three letters JVM, what’s the first thing that comes to mind? I generally have the following three kinds of people:

  • The first: JVM three letters, separate I know, a combination, I do not know what
  • The second kind: he is not Java virtual machine yao, run Java program
  • Third: divided into heap memory, method area, old age, new generation….. Blah blah blah… I could talk to you for hours

After my daily observation, generally the first kind of man is ape to the families of the program, the second person is program ape colleagues (not the Java work colleagues), the third person is the program ape apes, and suitable for the reader of this article is the third person, I try to from a different perspective, led application the apes are standing in a high point to other comprehensive rediscover a JVM, From shallow to deep step by step, once again we know the JVM to do a comprehensive combing, not only know that it has heap memory, method area, old age, new generation… It’s important to know how it works and how it works. On the basis of in-depth understanding of its design principles, and then use and tune it, it will be more handy.

Let me take you step by step into the world of the JVM! I will take you step-by-step through the JVM in a series of articles.

Disclaimer: The following content is long, please be patient to read, you will have a different harvest!

It all starts with the official website

So what’s the first step to getting to know the JVM? There are a lot of people who first learned about the JVM from Baidu or Google, but you are wrong about the first step, I think it is better to start from the official website, where it was born to do a comprehensive understanding.

Java Platform Standard Edition 8 Documentation

Website address: docs.oracle.com/javase/8/do…

Reference – > Developer Guides – > location: docs.oracle.com/javase/8/do…

Take the time and patience to read through this paragraph and you’ll learn something new!

Oracle has two products that implement Java Platform Standard Edition (Java SE) 8: Java SE Development Kit (JDK) 8 and Java SE Runtime Environment (JRE) 8.

JDK 8 is a superset of JRE 8, and contains everything that is in JRE 8, plus tools such as the compilers and debuggers necessary for developing applets and applications. JRE 8 provides the libraries, the Java Virtual Machine (JVM), and other components to run applets and applications written in the Java programming language. Note that the JRE includes components not required by the Java SE specification, including both standard and non-standard Java components.

The following conceptual diagram illustrates the components of Oracle’s Java SE products:

Getting Started with the G1 Garbage Collector

Website address: www.oracle.com/technetwork…

Exploring the JVM Architecture

Hotspot Architecture

The HotSpot JVM possesses an architecture that supports a strong foundation of features and capabilities and supports the ability to realize high performance and massive scalability. For example, the HotSpot JVM JIT compilers generate dynamic optimizations. In other words, they make optimization decisions while the Java application is running and generate high-performing native machine instructions targeted for the underlying system architecture. In addition, through the maturing evolution and continuous engineering of its runtime environment and multithreaded garbage collector, the HotSpot JVM yields high scalability on even the largest available computer systems.

The main components of the JVM include the class loader, the runtime data areas, and the execution engine.

Read and understand:

The original:

In other words, they make optimization decisions while the Java application is running and generate high-performing native machine instructions targeted for the underlying system architecture

Translation:

In other words, it makes optimization decisions while the Java application is running and generates high-performance local machine instructions for the underlying system architecture.

This means that the JVM’s greatest capability is to translate Java programs into local machine instructions that different underlying operating systems can understand and execute for the operating system to run.

The following diagram will be familiar to anyone who knows the JVM (a compilation runs everywhere) :

Source code to class files to bytecode

Let’s start with the simplest helloWorld.java, compile it, and get the helloWorld.class file. The above two steps are the most obvious to any Java program. One is the.java-ending source file, and the other is the.class ending bytecode file. We all know that a Java program must be compiled into a.class bytecode file by an editor in order to run. Before the JVM can run. But have you ever wondered how this bytecode file at the end of the.class runs and prints “Hello World” on the console?

Don’t worry, let me take you into the world of the JVM and see it from a different perspective through the following steps.

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World"); }}Copy the code

View a class file bytecode

Let’s take a look at the bytecode using the JDK’s built-in command hexdump -c helloworld.class.

Input the above command, output the following content, now this pile of things, you must not understand, don’t worry, first look at the official website is how to say, look at the following content.

00000000  ca fe ba be 00 00 00 34  00 22 0a 00 06 00 1409 |...4.."... | 00000010 00 15 00 16 08 00 17 0a 00 18 00 19 07 00 1a 07 |................ | 00000020 00 1b 01 00 06 3c 69 6e 69 74 3e 01 00 03 28 29 |..... 
      
       ... ()| 00000030 56 01 00 04 43 6f 64 65 01 00 0f 4c 69 6e 65 4e |V... Code... LineN| 00000040 75 6d 62 65 72 54 61 62 6c 65 01 00 12 4c 6f 63 |umberTable... Loc| 00000050 61 6c 56 61 72 69 61 62 6c 65 54 61 62 6c 65 01 |alVariableTable.| 00000060 00 04 74 68 69 73 01 00 0c 4c 48 65 6c 6c 6f 57 |.. this... LHelloW| 00000070 6f 72 6c 64 3b 01 00 04 6d 61 69 6e 01 00 16 28 |orld; . main... (| 00000080 5b 4c 6a 61 76 61 2f 6c 61 6e 67 2f 53 74 72 69 |[Ljava/lang/Stri| 00000090 6e 67 3b 29 56 01 00 04 61 72 67  73 01 00 13 5b |ng; )V...args...[| 000000a0 4c 6a 61 76 61 2f 6c 61 6e 67 2f 53 74 72 69 6e |Ljava/lang/Strin| 000000b0 67 3b 01 00 0a 53 6f  75 72 63 65 46 69 6c 65 01 |g; ...SourceFile.| 000000c0 00 0f 48 65 6c 6c 6f 57 6f 72 6c 64 2e 6a 61 76 |..HelloWorld.jav| 000000d0 61 0c 00 07 00 08 07 00 1c 0c 00 1d 00 1e 01 00 |a...............| 000000e0 0b 68 65 6c 6c 6f 20 77 6f 72 6c 64 07 00 1f 0c |.hello world....| 000000f0 00 20 00 21 01 00 0a 48 65 6c 6c 6f 57 6f 72 6c |. .! ...HelloWorl| 00000100 64 01 00 10 6a 61 76 61 2f 6c 61 6e 67 2f 4f 62 |d...java/lang/Ob| 00000110 6a 65 63 74 01 00 10 6a 61 76 61 2f 6c 61 6e 67 |ject...java/lang| 00000120 2f 53 79 73 74 65 6d 01 00 03 6f 75 74 01 00 15 |/System...out...| 00000130 4c 6a 61 76 61 2f 69 6f 2f 50 72 69 6e 74 53 74 |Ljava/io/PrintSt| 00000140 72 65 61 6d 3b 01 00 13 6a 61 76 61 2f 69 6f 2f |ream; ...java/io/| 00000150 50 72 69 6e 74 53 74 72 65 61 6d 01 00 07 70 72 |PrintStream...pr| 00000160 69 6e 74 6c 6e 01 00 15 28 4c 6a 61 76 61 2f 6c |intln...(Ljava/l| 00000170 61 6e 67 2f 53 74 72 69 6e 67 3b 29 56 00 21 00 |ang/String;)V.! .| 00000180 05 00 06 00 00 00 00 00 02 00 01 00 07 00 08 00 |................| 00000190 01 00 09 00 00 00 2f 00 01 00 01  00 00 00 05 2a |....../........*| 000001a0 b7 00 01 b1 00 00 00 02 00 0a 00 00 00 06 00 01 |................| 000001b0 00 00 00 06 00 0b 00 00 00 0c 00 01 00 00 00 05 |................| 000001c0 00 0c 00 0d 00 00 00 09 00 0e 00 0f 00 01 00  09 |................| 000001d0 00 00 00 37 00 02 00 01 00 00 00 09 b2 00 02 12 |...7............| 000001e0 03 b6 00 04 b1 00 00 00 02 00 0a 00 00 00 0a 00 |................| 000001f0 02 00 00 00 08 00 08 00 09 00 0b 00 00 00 0c 00 | |... 00000200 01 00 00 00 00 09 10 00 00 00 00 00 01 00 11 12 13. | | 00000210 00 00 02... 00 | | 00000215.
      Copy the code

The ClassFile Structure (byte code parsing)

Take a look at how bytecode files are explained:

Website address: docs.oracle.com/javase/spec…

A class file consists of a single ClassFile structure:

ClassFile {
    u4             magic;
    u2             minor_version;
    u2             major_version;
    u2             constant_pool_count;
    cp_info        constant_pool[constant_pool_count-1];
    u2             access_flags;
    u2             this_class;
    u2             super_class;
    u2             interfaces_count;
    u2             interfaces[interfaces_count];
    u2             fields_count;
    field_info     fields[fields_count];
    u2             methods_count;
    method_info    methods[methods_count];
    u2             attributes_count;
    attribute_info attributes[attributes_count];
}
Copy the code

Read the bytecode file

The first one, U4 Magic;

  • U4: represents 4 bytes (0x cafebabe), which is a magic number (has no business meaning, just indicates the type of file)

Basic knowledge:

A byte stores 8 bits of unsigned numbers ranging from 0 to 255. 1111 0000 (1 byte)

1Byte = 8bit

1Byte = 2 bits in hexadecimal

4Byte = 0x cafebabe

So u4 Magic, 0x cafebabe, converts to binary: 1100 1010 1111 1110 1011 1010 1011 1110

Second and third, u2 minor_version; u2 major_version; (Merge and combine to version number)

  • U2 minor_version: two bytes (0x 00 00), indicating the minor version number
  • U2 major_version: 2 bytes (0x 00 34), indicating the major version number
  • The major and minor versions are combined to form the version number, 0x 00000034 –> 52 in decimal notation.

Number four, U2 constant_pool_count; Represents constant quantity

constant_pool_count

The value of the constant_pool_count item is equal to the number of entries in the constant_pool table plus one. A constant_pool index is considered valid if it is greater than zero and less than constant_pool_count, with the exception for constants of type long and double noted in §4.4.5.

  • U2 constant_pool_count: 2 bytes (0x 00 22), converted to decimal: 34, the number of constants in the constant pool is 34-1=33
  • So what are the constants in this class? Look at the following array of constants:

Cp_info constant_pool[constant_pool_count-1]; Represents constant array

constant_pool[]

The constant_pool is a table of structures (§4.4) representing various string constants, class and interface names, field names, and other constants that are referred to within the ClassFile structure and its substructures. The format of each constant_pool table entry is indicated by its first “tag” byte.

The constant_pool table is indexed from 1 to constant_pool_count – 1.

  • Each constant has the form:

The Constant Pool

Java Virtual Machine instructions do not rely on the run-time layout of classes, interfaces, class instances, or arrays. Instead, instructions refer to symbolic information in the constant_pool table.

All constant_pool table entries have the following general format:

cp_info {
    u1 tag;      // Indicates the type of a constant
    u1 info[];   / / said.
}
Copy the code

The first constant:

  • U1 tag: represents 1 byte (0x 0a), converted to decimal value: 10, find the following table, represents a method type constant
// Method type constants
CONSTANT_Methodref_info {
    u1 tag;
    u2 class_index;
    u2 name_and_type_index;
}
Copy the code
A constant number u1 tag Constant type Constant content
# 1 0x 0a–>10 CONSTANT_Methodref U2 class_index: 0x 00 06–> #6

U2 name_AND_type_index: 0x 00 14 –> #20

# 6. # 20

Other constant parsing :(follow the above method, step by step, you can parse this table)

So far, you still can’t see what this is all about. Take your time and look at the following command.

A constant number u1 tag Constant type Constant content
# 1 0x 0a–>10 CONSTANT_Methodref U2 class_index: 0x 00 06–> #6

U2 name_AND_type_index: 0x 00 14 –> #20

# 6. # 20
# 2 0x 09–>9 CONSTANT_Fieldref U2 class_index: 0x 00 15–> #21

U2 name_AND_type_index: 0x 00 16-> #22

# 21, # 22
# 3 0x 08–>8 CONSTANT_String U2 string_index: 0x 00 17–> #23
# 4 0x 0a–>10 CONSTANT_Methodref U2 class_index: 0x 00 18–> #24

U2 name_AND_type_index: 0x 00 19 –> #25

# 24. # 25
.
# 33

Javap disassembly

  • Command: javap -v -p -c helloWorld.class > helloWorld_javap.txt (disassemble the HelloWorld bytecode file and write the generated content to helloWorld_javap.txt)
// The following is an excerpt:Classfile /... /HelloWorld.class Last modified2021-9-28; size 533 bytes
  MD5 checksum c3ef731e76bb9c516e7840a04a0c71af           //<-----------magic
  Compiled from "HelloWorld.java"
public class HelloWorld
  minor version: 0
  major version: 52 //<----------------- version numberflags: ACC_PUBLIC.ACC_SUPER
Constant pool# 1:= Methodref          #6.#20         // java/lang/Object."<init>":()V
   #2 = Fieldref           #21.#22        // java/lang/System.out:Ljava/io/PrintStream;
   #3 = String             #23            // hello world
   #4 = Methodref          #24.#25        // java/io/PrintStream.println:(Ljava/lang/String;) V
   #5 = Class              #26            // HelloWorld
   #6 = Class              #27            // java/lang/Object
   #7 = Utf8               <init>
   #8 = Utf8               ()V
   #9 = Utf8               Code
  #10 = Utf8               LineNumberTable
   ...
   #33.Copy the code

summary

From the above analysis, we know that a Java source file needs to be compiled into a class file before the JVM can actually run this code. Next, we will examine the Java class file loading mechanism.

Stay tuned for chapter 2 of the JVM series on the JVM class loading mechanism (interpreted from a different perspective)!

Recommended reading

Guava Cache actual Combat – From scenario to principle analysis

Details of HTTP2.0 and HTTPS protocols

, recruiting

Zhengcaiyun Technology team (Zero) is a passionate, creative and executive team based in picturesque Hangzhou. The team has more than 300 r&d partners, including “old” soldiers from Alibaba, Huawei and NetEase, as well as newcomers from Zhejiang University, University of Science and Technology of China, Hangzhou Electric And other universities. Team in the day-to-day business development, but also in cloud native, chain blocks, artificial intelligence, low code platform system, middleware, data, material, engineering platform, the performance experience, visualization technology areas such as exploration and practice, to promote and fell to the ground a series of internal technical products, continue to explore new frontiers of technology. In addition, the team is involved in community building, Currently, There are Google Flutter, SciKit-Learn, Apache Dubbo, Apache Rocketmq, Apache Pulsar, CNCF Dapr, Apache DolphinScheduler, and Alibaba Seata and many other contributors to the excellent open source community. If you want to change something that’s been bothering you, want to start bothering you. If you want to change, you’ve been told you need more ideas, but you don’t have a solution. If you want change, you have the power to make it happen, but you don’t need it. If you want to change what you want to accomplish, you need a team to support you, but you don’t have the position to lead people. If you want to change the original savvy is good, but there is always a layer of fuzzy window…… If you believe in the power of believing, believing that ordinary people can achieve extraordinary things, believing that you can meet a better version of yourself. If you want to be a part of the process of growing a technology team with deep business understanding, sound technology systems, technology value creation, and impact spillover as your business takes off, I think we should talk. Any time, waiting for you to write something and send it to [email protected]

Wechat official account

The article is published synchronously, the public number of political cloud technology team, welcome to pay attention to