Discussion on virtual machine architecture of Java Virtual machine

Source: Internet
Author: User

Since the work above, the program because the code more write more, become more and more bloated, the efficiency will become lower, so I always like to constantly optimize the program structure outside, memory optimization and performance tuning.

To make memory optimizations and performance tuning for Java programs, you need to understand the internal principles of virtual machines and understand the benefits of Java virtual machines in addition to the two points mentioned above. 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

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/0B/wKioL1VyotSAjt7TAAD7miYMhjA634.jpg "title=" 11.jpg "alt=" Wkiol1vyotsajt7taad7miymhja634.jpg "/>

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:
load --finds and loads binary data of type;
Connect -perform validation, prepare and parse (optional)

Verify that the type being imported is correct
prepare to allocate memory for class variables and initialize them to default values
Parse 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

Java code 650) this.width=650; "class=" star "src=" Http://yshjava.iteye.com/images/icon_star.png "alt=" Favorite Code "style=" border:0px; "/>

    1. //Returns the ClassLoader reference that was just saved

    2. String. class. getClassLoader ();

A reference to Class


Java code 650) this.width=650; "class=" star "src=" Http://yshjava.iteye.com/images/icon_star.png "alt=" Favorite Code "style=" border:0px; "/>

    1. //will return references to the class class that you just saved

    2. String. class;

Note that the method area can also be reclaimed by the garbage collector, and when a type is no longer referenced and the method area is low on memory, the virtual machine unloads the type and reclaims the memory.

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 only one heap space, and all threads share this 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:


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/0B/wKioL1VyovezeDdpAAFNZwXAWOo243.jpg "title=" 22.jpg "alt=" Wkiol1vyovezeddpaafnzwxawoo243.jpg "/>

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

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/0F/wKiom1VyoZ_QrIQrAADSRQFh6Mg605.jpg "title=" 33.jpg "alt=" Wkiom1vyoz_qriqraadsrqfh6mg605.jpg "/>

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.


This article is from the Java Learning Video tutorial blog, so be sure to keep this source http://10239772.blog.51cto.com/10229772/1659194

Discussion on virtual machine architecture of Java Virtual machine

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.