Java virtual machine architecture

Source: Internet
Author: User
Lifecycle of a Java Virtual Machine

The duty of a runtime Java Virtual Machine instance is to run a JavaProgram. When a Java program is started, a virtual machine instance is born. When the program is closed and exited, the virtual machine instance will die. If three Java programs run on the same computer at the same time, three Java Virtual Machine instances will be obtained. Each Java program runs on its own Java Virtual Machine instance.

A Java Virtual Machine instance runs a Java program by calling the main () method of an initial class. The main () method must be public, static, and return value void. A string array is used as a parameter. Any class that has such a main () method can be used as the starting point for Java program running.

 
Public ClassTest {Public Static VoidMain (string [] ARGs ){//Todo auto-generated method stubSystem. Out. println ("Hello World");}}

In the above example, the main () method in the Java program's initial class serves as the starting point of the program's initial thread, and any other threads are started by this initial thread.

There are two types of threads in the Java Virtual Machine: the daemon thread and the non-daemon thread. Daemon threads are generally used by virtual machines, such as threads that execute garbage collection tasks. However, a Java program can mark any thread it creates as a daemon thread. The initial thread in the Java program is the one starting from main () and is not a daemon thread.

As long as there are any non-daemon threads running, the Java program continues to run. When all the non-daemon threads in the program are terminated, the VM instance automatically exits. If the security manager permits, the program can also exit by calling the exit () method of the runtime class or system class.

Architecture of Java Virtual Machine

Is the structure of the Java Virtual Machine. Each Java virtual machine has a class loading subsystem, which loads the type (class or interface) according to the given full qualified name ). Similarly, each Java virtual machine has an execution engine that executes commands contained in the methods of the loaded class.

When a Java virtual machine runs a program, it needs memory to store many things, such: bytecode, other information obtained from the mounted class file, objects created by the program, parameters passed to the method, return values, local variables, and so on. The Java Virtual Machine organizes these items into several "RunTime data zones" for ease of management.

Some runtime data zones are shared by all threads in the program, and others can only be owned by one thread. Each Java Virtual Machine instance has a method zone and a heap, which are shared by all threads of the Virtual Machine instance. When a virtual machine loads a class file, it parses the type information from the binary data contained in this class file. Then place the type information in the method area. When the program is running, the virtual opportunity puts all the objects created during the program running in the heap.

When a new thread is created, it will get its own PC register (program counter) and a java stack. If the thread is executing a Java method (non-local method ), the value of the PC register will always point to the next instruction to be executed, and its java stack will always store the status of Java method calls in the thread-including its local variables, parameters, return values, and intermediate calculation results passed in when called. The local Method Invocation status is stored in the local method stack as a method dependent on the specific implementation, or in registers or some other memory areas related to the specific implementation.

A java stack is composed of many stack frames. A stack frame contains the status of a Java method call. When a thread calls a Java method, the VM pushes a new stack frame to the java stack of the thread. When the method returns, this stack frame is popped up from the java stack and discarded.

Java virtual machine does not have registers, and its instruction set uses java stack to store intermediate data. The reason for this design is to keep the instruction set of the Java Virtual Machine as compact as possible and make it easier for the Java Virtual Machine to implement on platforms with few General registers. In addition, the stack-based architecture of Java virtual machine also helps dynamic compilers and real-time compilers implemented by some virtual machines during runtime.CodeOptimized.

It depicts the memory areas created by the Java Virtual Machine for each thread. These memory areas are private and no thread can access the PC register or java stack of another thread.

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.