1. Introduction to the JVM

  • Virtual machine concept:

    Virtual Machine (Virtual Machine) refers to a complete computer system with complete hardware system functions simulated by software and running in a completely isolated environment. Anything that can be done in a physical computer can be done in a virtual machine. When creating a VM on a computer, use part of the hard disk and memory capacity of the physical machine as the hard disk and memory capacity of the VM. Each VIRTUAL machine has an independent CMOS, hard disk, and operating system. You can operate the virtual machine just like a physical machine.

  • Key words:

    Chinese name Foreign names define
    The virtual machine Virtual Machine A complete computer system with complete hardware system functions
    technology And entity machine difference Common VMS
    A type of virtualization technology It can be used just like a physical machine Java vm, Linux VM, Windows VM
  • Vm Classification

    • Linux virtual machine

      A virtual Linux operating environment installed on Windows is called a Linux virtual machine. It’s really just a file, a virtual Linux environment, not a real operating system. But the actual effect is the same. So install in the virtual machine use well. The commonly used VM software includes vmware and VirtualBox.

    • The Java virtual machine

      Java Virtual Machine (JVM), short for Java Virtual Machine, is a fictitious computer that is implemented by emulating various computer functional simulations on a real computer. Java virtual machine has its own perfect hardware architecture, such as processor, stack, register, and the corresponding instruction system.

  • Advantages of the Java virtual machine

    A very important feature of the Java language is its platform-independent nature. The use of the Java virtual Machine is key to this. Normal high-level languages need to be compiled into different object code if they are to run on different platforms. With the introduction of the Java language virtual Machine, 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 across multiple platforms by simply generating object code (bytecode) that runs on the Java virtual machine. When the Java virtual Machine executes bytecode, it interprets the bytecode to be executed as machine instructions on the specific platform.

    In short: compile once, run everywhere, with automatic memory management and automatic garbage collection

  • conclusion

    The Java Virtual Machine is the foundation of the underlying implementation of the Java language, and anyone interested in the Java language should have a general understanding of the Java Virtual Machine. This helps you understand some of the properties of the Java language, and it helps you use it. Software people who want to implement the Java Virtual Machine on a particular platform, compiler writers for the Java language, and those who want to implement the Java Virtual Machine on a hardware chip must have a deep understanding of the Java Virtual Machine specification. In addition, if you want to extend the Java language, or compile other languages into Bytecode in the Java language, you also need a deep understanding of the Java Virtual Machine.

Common JVMS

Many companies have their own JVMS, such as Orcale’s HotSpot, which is the focus of our study and use.

【 Introduction to the other two special virtual Machines 】

  1. Sun Classic VM Introduction:

    Back in 1996 with Java1.0, SUN released a Java virtual machine called SUN Classic VM, which was the first commercial Java virtual machine in the world, and was completely phased out by JDK1.4. The virtual machine provides only an interpreter internally; if you use a JIT compiler, you need to plug it in. But once the JIT compiler is used, the JIT takes over the executing system of the virtual machine and the interpreter no longer works. The interpreter and compiler do not work together.

    Hotspot now has this virtual machine built in.

  2. Dalvik VM:

    Google’s development of virtual machines for the Android operating system, with JIT available in Android2.2, is booming.

    The Dalvik VM does not execute Java Class files directly because it does not follow the Java Virtual Machine specification and is based on the register structure rather than the JVM stack architecture. Therefore, it executes a compiled dex (Dalvik Executable) file (which can be converted through Class files), with high execution efficiency.

    Android5.0 uses an ART VM that supports Ahead of Time Compilation (AOT) instead of the Dalvik VM.

Our roadmap is to learn how Java makes code work, starting with the loading of a class, and getting a deeper understanding of the JVM as a whole from point to point.

3. How Java code works

  • How does the Java code we usually write run?

    As we all know, we usually create a class with a.java file name on the local disk, such as user.java or product.java, which is also called a source code file.

    These source files must be compiled by our Javac tool to produce.class bytecode files before they can be run.

Now we have to wonder: how do these.class bytecode files work?

(Here we can start by executing Java commands from the DOS window)

Once you use a Java command at this point, you actually start a JVM process that is responsible for loading these bytecode files into memory for execution.

The process of loading a class bytecode file into the memory of the virtual machine is called class loading, which involves the concept of class loading mechanism and class loader.

When the bytecode file is loaded into the MEMORY of the JVM by the classloader, the JVM’s execution engine will execute the corresponding class in memory. For example, the main method of the class will be executed first. If there are other object references in the main method, the class load will start loading the corresponding bytecode file into memory. The JVM then executes the call. (Figure below)

Ok, through the above analysis, we can first make a comprehensive introduction to the overall running process of Java code, and then in-depth analysis of how the class loader is implemented, step by step in-depth study.