Hello, today I’m going to share with you JVM. Take out your notebook and write it down

1. What is Jvm

The JVM, short for Java Virtual Machine, is a specification for computing devices. The JVM is an imaginary computer that is implemented by emulating various computer functions on an actual computer.

With the introduction of Java language virtual machines, the Java language does not need to be recompiled when running on different platforms. The Java language uses the Java Virtual machine to mask platform-specific information, allowing Java language compilers to run unmodified on multiple platforms by generating object code (bytecode) that runs on the Java Virtual machine.

The Java virtual machine has its own complete hardware architecture, such as processor, stack, and corresponding instruction system.

A Java virtual machine is essentially a program that, when launched on the command line, begins executing instructions stored in a bytecode file. The portability of the Java language is based on the Java virtual machine. Bytecode files (.class) can run on any platform that has a Java virtual machine specific to that platform. This is “compile once, run many times”.

Java virtual machine is not only a cross-platform software, but also a new network computing platform. The platform includes many related technologies, such as various apis that conform to open interface standards, optimization techniques, and so on. Java technology enables the same application to run on different platforms. The Java platform can be divided into two parts, namely the Java Virtual Machine (JVM) and the Java API class library

Java VIRTUAL machines are divided into five modules:

  • Class loader subsystem
  • Runtime data area
  • Execution engine
  • Local method interface
  • Garbage collection module

2. Of the Jvm

2.1 Jdk architecture

2.2 Jvm Architecture

  • Class loader

  • Thread shared area

  • Methods area

  • The heap

  • Thread private area

  • The virtual machine stack

  • Local method stack

  • Program counter

3. Class loading mechanism

3.1 process

Load >> Verify >> Prepare >> Parse >> Initialize >> Use >> Uninstall

Load: a bytecode file is searched on the hard disk and read through IO. It is loaded only when the Class is used, such as calling the main() method of the Class, the new object, etc. During the load phase, a java.lang.Class object representing the Class is generated in memory, which acts as an access point to the various data of the Class in the method area

Validation: Verifies the correctness of the bytecode file

Preparation: Allocates memory to static variables of the class and assigns default values

Resolution: the replacement symbol references for direct reference to the stage will put some static methods (symbol references, such as the main () method) to replace for the pointer to the data storage, memory, or a handle, etc. (reference) directly, this is the so-called static linking process (complete) during class loading, dynamic linking is done during the program is running will use replacement for direct reference symbols

Initialization: Executes a static block of code to initialize a class’s static variable to the specified value

Classes loaded into the method area mainly contain runtime constant pool, type information, field information, method information, class loader

References, references to the corresponding class instance, etc.

Class loader reference: A reference from this class to an instance of the class loader

Reference to a class instance: The class loader creates a class of the corresponding class type after the class information is placed in the method area

Object instances are placed in the Heap as an entry point and pointcut for developers to access class definitions in the method area.

3.2 Class loaders and parent delegate mechanisms

The above class loading process is mainly implemented through class loaders, and there are several types of loaders in Java

Bootloader: Load the core libraries that support the JVM in the JRE’s lib directory, such as rt.jar, charsets.jar, etc

Extension class loader: Is responsible for loading JAR classes from the Ext extension directory in the JRE’s lib directory that support the JVM

The application class loader is responsible for loading classes in the ClassPath path, mainly those you write yourself

Custom loader: loads class packages in user-defined paths

3.3 Class loader initialization process

The JVM Launcher instance Sun.misc.Launcher is created.

The sun.misc.Launcher initialization uses a singleton design that ensures that there is only one instance of Sun.misc.Launcher in a JVM.

Within the Launcher construction method, it creates two class loaders, respectively is sun misc. The Launcher. ExtClassLoader (extension class loader) and sun. Misc. The Launcher. AppClassLoader (application class loader).

By default, the JVM loads our application using an instance of the AppClassLoader returned by the getClassLoader() method of the Launcher.

3.4 Parent delegation mechanism

JVM classloaders have a parent-child hierarchy, as shown below

In fact, there is a parent delegation mechanism for class loading. When a class is loaded, it first entrusts the parent loader to find the target class, and then entrusts the upper parent loader to load it. If all the parent loaders cannot find the target class in their own class loading path, they will find and load the target class in their own class loading path.

The parent delegate mechanism is simply that the father loads it first, and if not, the son loads it

3.5 Why design parent delegation mechanism?

  • Sandbox security: Self-written java.lang.String.class classes are not loaded, which prevents the core API library from being tampered with
  • Avoid class reloading: When the parent has already loaded the class, there is no need for the child ClassLoader to load it again to ensure the uniqueness of the loaded class

3.6 Be fully responsible for the entrustment mechanism

“Full responsibility” means that when a ClassLoder loads a class, unless another ClassLoder is displayed, the classes that the class depends on and references are also loaded by that ClassLoder.

4. Regions of the Jvm are concatenated

5. Summary

After reading this chapter, you should have a general understanding of the Jvm and its structure, which I hope will help you confused at the screen