Deep understanding of what a Java virtual machine really is

Source: Internet
Author: User

Original link: http://blog.csdn.net/zhangjg_blog/article/details/20380971 What is a Java virtual machine as a Java programmer, we write every dayJava code, the code we write is executed on something called a Java virtual machine. But if you want to ask what is a virtual machine, I'm afraid a lot of people will be ambiguous. In this article, I'll write down my understanding of virtual machines. Because of the limitations of capacity, some places may not be described enough. If you have a different understanding, welcome to communicate.

We all know that a Java program must run on a virtual machine. So what exactly is a virtual machine? First look at the online search for more reliable explanation:

virtual machine is an abstract computer java virtual machine has its own perfect hardware schemas, such as processor register directive system. jvm operating system platform-related information so that Span lang= "en-US" >java program just generate in java The target code running on the virtual machine (

This explanation should be correct, but only describes the external behavior and functionality of the virtual machine, and does not explain the internal principles. In general, we do not need to know the operating principle of virtual machines, as long as the focus on Java code, this is the reason why the virtual machine exists -shielding the underlying operating system platform and reduce the complexity based on native language development, so that The Java language is easy to use across a wide range of platforms (as long as virtual machine vendors implement virtual machines on a specific platform). These are the external features of the virtual machine, but it is too general to interpret the virtual machine from this information to let us know the internal principle.



Explain from the perspective of the processJVM 

Let's try to understand the virtual machine from the operating system level. We know that the virtual machine is running in the operating system, so what can be run in the operating system? Process, of course, because the process is the execution unit in the operating system. It can be understood that when it is running, it is a process instance in an operating system, and when it is not running (as an executable file in the file system), it can be called a program.


To the command line is familiar with the classmate, all know that a command corresponds to an executable binary file, when the command is knocked down and enter, will create a process, load the corresponding executable file into the address space of the process, and execute the instructions therein. The following compares the C and Java language HelloWorld programs to illustrate the problem.


First write the C language version of the HelloWorld program.

[CPP]View PlainCopy
    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. int main (void) {
    4. printf ("Hello world\n");
    5. return 0;
    6. }

Compile the C language version of the HelloWorld program:

[Plain]View PlainCopy
    1. GCC Helloworld.c-o HelloWorld

Run the C-language version of the HelloWorld program:

[Plain]View PlainCopy
    1. [Email protected]:/deve/workspace/helloworld/src$./helloworld
    2. Hello World


The GCC compiler compiles the file directly as a binary executable that can be recognized by the operating system when we hit it at the command line./helloworld This command, create a process directly, and load the executable into the process's address space to execute the instructions in the file.

As a comparison, let's look at the compilation and execution forms of the Java version of the Helloword program.

First write the source file Helloword.java:

[Java]View PlainCopy
    1. Public class HelloWorld {
    2. public static void Main (string[] args) {
    3. System.out.println ("HelloWorld");
    4. }
    5. }


Compile the Java version of the HelloWorld program:

[Java]View PlainCopy
    1. ZHANGJG@linux:/deve/workspace/hellojava/src$ javac Helloworld.java
    2. ZHANGJG@linux:/deve/workspace/hellojava/src$ ls
    3. HelloWorld. class Helloworld.java


Run the Java version of the HelloWorld program:

[Plain]View PlainCopy
    1. [Email protected]:/deve/workspace/hellojava/src$ java-classpath. HelloWorld
    2. HelloWorld


As you can see from the above procedure, when we run the Java version of the HelloWorld program, the command typed is not./helloworld.class. Because class files are not binary executables that can be directly recognized by the operating system. We typed in the Java command. This command shows that we start with a program called Java, which is a JVM process instance after it is run.

The above command execution process is this:

JavaCommand starts the virtual machine process first, after the virtual machine process starts successfully,Reading parameters“helloworld "main.class files are not directly loaded directly in cpuclass files, including jdk stringobject class translate these bytecode instructions into the cost machine cpu can recognize the instructions in order to cpu< Span lang= "ZH-CN" to run on.

From this point of view, when executing a so-called Java program, what really is executing is a process called a Java virtual machine, not one of the class files we write. This process, called a virtual machine, handles some of the underlying operations, such as allocating and releasing memory, and so on. The class file We write is just the "raw material" needed for the virtual machine process to execute. These "raw materials" are loaded into the virtual machine at runtime and interpreted by the virtual machine to control the virtual machine implementation of some of the relatively high-level operations defined in our Java code, such as creating a file, etc., you can view the information in the class file as control information for the virtual machine. That is, a virtual instruction.

Programming languages also have their own principles, learning a language, mainly to understand its principles. Seemingly a simple HelloWorld program, there are a lot of in-depth content worth dissecting.

Introduction to the JVM architecture

In order to demonstrate the relationship between the virtual machine process and the class file, the following picture is drawn:



According to the content of the expression, we compiled the class file as a Java Virtual machine raw material is entered into the internal Java virtual machine, then who will do this part of the work? In fact , inside a Java virtual machine, there is a subsystem called the ClassLoader, which is used to load classes as needed at run time. Notice the "as needed" four words in the above sentence. During the execution of the Java Virtual machine, only when he needs a class will the ClassLoader be called to load the class, and all classes will not be loaded at the beginning of the run. Like a person, only when hungry to eat, rather than once a year of food to eat into the belly. In general, the time for a virtual machine to load a class is when a new class is used for the first time. The following article in this column will discuss the class loader in Java in detail.


After the class loaded by the virtual machine is loaded into the Java Virtual machine memory, the virtual opportunity reads and executes the bytecode instructions that exist within it. The part of the virtual machine that executes the bytecode instruction is called the execution engine. Like a person, not to eat down on the finished, but also to digest, the implementation of the engine is equivalent to the human gastrointestinal system. Each class file is also dynamically connected during the execution . The specific behavior of the execution engine and the content of the dynamic link are also discussed in subsequent articles in this column.

We know thatJava virtual opportunities are automated memory management. Specifically, it automatically frees unused objects without requiring programmers to write code to free allocated memory. This part of the work is the responsibility of the garbage collection subsystem.

From the above discussion, we can know that a Java virtual machine instance has three subsystems to ensure its normal operation, namely the ClassLoader subsystem, the execution engine subsystem and the garbage collection subsystem. As shown in the following:



To run a virtual machine, you must load the class file and execute the bytecode directive in the class file. It does so many things and must need its own space. Just like what people eat, put them in the stomach first. Virtual machines also need space to store the data. First, the loaded bytecode requires a separate memory space to store; a thread's execution also requires memory space to maintain the method's call relationship, the data in the method, and the intermediate calculation results; In the process of execution, it is unavoidable to create objects that require a dedicated memory space to store. The contents of the data area of the virtual runtime will also appear in subsequent articles in this column. The run-time memory area of a virtual machine can probably be divided into several parts as shown. (This is just about the division, and it's not very finely divided)

Summarize

Written here, basically about my understanding of the Java Virtual machine is finished. The topic of this article, though, is a deep understanding of Java virtual machines, but you may not feel a bit "in-depth", just a generalities. I have this feeling, too. Limited to their own level, can only do so, if you want to understand the Java virtual machine, it is strongly recommended to read three books:


"Deep Java Virtual machine"

Deep understanding of Java Virtual machine JVM advanced features and best practices

Java Virtual Machine specification


I've actually read these books, but their interpretation of virtual machines is also based on an external model, without deep analysis of the implementation principle inside the virtual machine. Virtual machine is a big and complex thing, the realization of the virtual machine is the people of the level, if not participate in the implementation of virtual machines, it should be very few people can be understood. Some of the articles later in this column also refer to these three books, although there are countless books on Java syntax, but in-depth explanations of virtual machine books, so far I have seen these three, and the online information is not many.

Finally make a summary:

1 virtual machines are not mysterious, and in the view of the operating system, it is just a normal process.

2 This process, called a virtual machine, is special, it can load the class file we write. If the JVM is likened to a person, then the class file is the food we eat.

3 The class file is loaded with a subsystem called the ClassLoader. Just like our mouths, we eat our food in our stomachs.

4 the execution engine in the virtual machine is used to execute bytecode instructions in the class file. Like our stomachs, we digest the food we eat.

5 during execution, the virtual machine allocates memory to create the object. When these objects become obsolete and useless, they must be cleaned up automatically. The task of cleaning up objects to reclaim memory is the responsibility of the garbage collector. Like the food that people eat, after digestion, the waste must be discharged out of the body, making room to eat and digest food the next time you are hungry.

Deep understanding of what a Java Virtual machine really is (go)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.