Java Virtual machine Architecture in-depth research summary

Source: Internet
Author: User

Since the work, the code more write more, the program is more and more bloated, inefficient, for me such a pursuit of perfect programmer, this is absolutely not allowed, so in addition to constantly optimize the program structure, memory optimization and performance tuning is my usual "trick."

To do memory optimization and performance tuning of Java programs, not understanding the internal principles of virtual machines (or even more stringent specifications) is certainly not possible, here is a good book "Deep Java Virtual Machine (second edition)" (Bill Venners, Cao Xiaogang Shang translation, actually this article is the author after reading this book , a personal understanding of the Java Virtual machine). Of course, understanding the benefits of Java virtual machines is not limited to these two benefits. At a deeper level of technology, understanding the specifications and implementations of Java virtual machines will help us write efficient and stable Java code. For example, if you understand the memory model of a Java virtual machine and understand the memory recovery mechanism of a virtual machine, then we will not rely too much on it and will explicitly "free up memory" when needed (Java code cannot explicitly free memory, but you can tell the garbage collector to reclaim the object by releasing the object reference). To reduce unnecessary memory consumption; If we understand how the Java stack works, we can reduce the risk of stack overflow by reducing the number of recursive layers and reducing the number of cycles. It may not be possible for an application developer to directly address the underlying implementation of these Java virtual machines, but understanding these background knowledge, more or less, will have a subtle and good effect on the programs we write.

This article, will be concise description of the Java Virtual Machine architecture and memory model, such as useful words or interpretation of inaccuracies, please do not hesitate to correct, deeply honored!

Java Virtual Machine Architecture

Class loading Subsystem

The Java Virtual machine has two kinds of loaders, namely the Startup class loader and the user custom loader.

The pass-class loading subsystem loads the class into the runtime data area through the fully qualified name (package name and class name, and the network load also includes the URL) of the classes. For each loaded type, the Java virtual machine creates an instance of the Java.lang.Class class to represent the type, which is placed in the in-memory heap area, and the type information that is loaded is in the method area, which is the same as all other objects.

Class mount subsystem before loading a type, in addition to locating and importing the corresponding binary class file, verify the correctness of the imported classes, allocate and initialize memory for class variables, and parse symbol references as direct references, these actions are performed in the following order:

1) Load--Find and load binary data of type;

2) Connect--perform validation, prepare and parse (optional)

3) Verify that the type being imported is correct

4) Prepare to allocate memory for class variables and initialize them to default values

5) parsing converts a symbol reference in a type to a direct application

Method area

For each type loaded by the class mount subsystem, the virtual machine will save the following data to the method area:

    • Fully qualified name of the type
    • Fully qualified name of type superclass (Java.lang.Object no superclass)
    • Type is a class type or an interface type
    • Type of access modifier
    • An ordered list of fully qualified names for any direct hyper-interface
In addition to the basic type information above, the following information will be saved:
    • Constant pool of type
    • Field information (including field name, field type, field modifier)
    • method information, including the method name, return type, number and type of parameters, method modifiers, if the method is not abstract and local, also saves the method's bytecode, the operand stack, and the size of the local variable area in the method stack frame and the exception table.
    • All class variables except constants are actually static variables of the class, because the static variables are shared by all instances and are directly related to the type, so they are class-level variables that are stored as members of the class in the method area.

A reference to the class ClassLoader

Returns the ClassLoader reference String.class.getClassLoader () that was just saved   ;

A reference to Class

Returns the reference string.class for the class you just saved   ;

Note that the method area can also be reclaimed by the garbage collector.

Heap

All class instances or arrays created at run time by a Java program are placed in the same heap, and each Java virtual machine has a pair of spaces, and all threads share a heap (which is why a multithreaded Java program produces synchronization problems with object access).

Since each Java virtual machine has a different implementation of the virtual machine specification, we may not know how each type of Java Virtual machine represents an object instance in the heap, but we can get a glimpse of this possible implementation:

Program counter

For a running Java program, each thread has its own PC (program counter) register, which is created at the start of the thread and is a word length that holds the location of the next line of code that needs to be executed.

Java stack

Each thread has a Java stack that holds the running state of the thread as a stack frame. There are two types of virtual machine operations on the Java stack: the stack and the stack, both of which are frame units. The stack frame holds data such as incoming parameters, local variables, intermediate operation results, and is ejected when the method is completed, and then released.

Take a look at the memory snapshot of the stack frame when the two local variables are added

Local method Stack

This is where Java calls the operating system's local library to implement JNI (Java Native Interface,java local interface)

Execution engine

The core of the Java Virtual machine, which controls loading Java bytecode and parsing; for a running Java program, each thread is an instance of a separate virtual machine execution engine, from the beginning of the thread's life cycle to the end, either executing the bytecode or executing the local method.

Local interface

The local method stack and the operating system library are connected.

Note: All of the references to "Java virtual machines" in this article refer to the Java Virtual Machine specification for EE and javase platforms.

Java Virtual machine Architecture in-depth research summary

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.