Inside the Java Virtual Machine reader, insidevirtual

Source: Internet
Author: User

Inside the Java Virtual Machine reader, insidevirtual
What?

Jvm may refer:

  • The abstract specification,
  • A concrete implementation, or
  • A runtime instance.

A running Virtual Machine instance (a runtime instance) is responsible for running a java program. This instance is generated as the java program starts (the main method runs) and disappears.

Java Virtual Machine has two types of threads: daemon thread and non-daemon thread. backend threads such as gc are daemon threads, and the main method produces non-daemon threads. When all the non-daemon threads end, the jvm ends.

JVM (the abstract specification) defines the abstract components and the interaction between them to define JVM implementation (a concrete implementation ):

Each JVM hasClass loader subsystem,When a class or interface (with a fully qualified name) is loaded to the JVM, it is loaded to the class loader subsystem. Similarly, each JVM hasExecution engineTo execute the method commands in the loaded class.

. Class file and byteCode (byteCode)

The Class package contains code and metadata (metadata) that supports code ). The Code part is the bytecode, while the metadata part includes many information such as generics. Therefore, the Class file contains bytecode, but not only bytecode.

Class Loader subclass

Takes care of finding and loading types

Jvm has two types of loaders: a primordial class loader and class loader objects (original class loader: Part of jvm, custom class loader object: Part of java program ). The Class Loader object (java program) must inherit from
Interface methods defined in java. lang. ClassLoader. ClassLoader provide JVM access mechanisms. For each mounted type in JVM, a java is generated. lang. the class Object represents it. Like other Object objects, the Object is generated in Heap, And the type information is stored in Method Area.

In addition to locating and importing binary Class files, the Class Loader must also verify the correctness of the import Class, allocate Class variables and initialize the memory..

General Order:

Runtime data areas

When the JVM is running, it needs to be stored in memory, such as bytecode and other information obtained from the class file, Object Instantiation, method parameters, return values, and local variables, media of computing results. ThereforeEach JVM defines multiple runtime data areasOrganize the memory required by these execution programs. (Various JVM will generate different implementations based on these abstract definitions ).

Method Area & Heap

Each JVM instance hasOne Method Area and one HeapThe MethodArea and Heap areAll threads in the JVM are shared.. When a JVM uses classLoader to correctly locate and load the class file, it will parse the type information from the binary information and put the type information into MethodArea, and put the object instance in Heap.

I mentioned that method area mainly places type information. The so-called type information refers:

Type Information

For each type it loads, a Java Virtual Machine must store the following kinds of information in
Method area:

  • The fully qualified name of the type

  • The fully qualified name of the type ís direct superclass (unless the type is an interface or class java. lang. Object, neither of which have a superclass)

  • Whether or not the type is a class or an interface

  • The type ís modifiers (some subset of 'public, abstract, final)

  • An ordered list of the fully qualified names of any direct superinterfaces

In fact, through the java. lang. Class method, we can dynamically obtain some type information stored in MethodArea:

public String getName();public Class getSuperClass();public boolean isInterface();public Class[] getInterfaces();public ClassLoader getClassLoader()

In addition, MethodArea also includes the following:

  • The constant pool for the type
    Each Type (class or interface) has a constant pool. It is an ordered set that stores references of basic types, references of types (classes or interfaces), Class variable references, and method references.

  • Field information
    Stores information about class variables: the type, name, and modifier of class variables (such as private, public, final, and static)

  • Method information
    Method Name, method return value, method modifier, method parameter list, method bytecode, and method throw exception list (non-abstract and native methods ).

  • All class (Static Fields) variables declared in the type, counter t constants

  • A reference to class ClassLoader
    JVM needs to track whether the type (type, class or interface) is loaded into the object of the original classLoader or classLoader. Therefore, method Area needs to store a reference of classloader.

  • A reference to class Class
    JVM creates a java. lang. class instance for each loaded type (interface \ class. In addition, JVM also needs to associate this java. lang. class instance with its Type Information (which exists with MethodArea) in some way.
    Through this static method, you can obtain any java. lang. class instance corresponding to the type loaded into the classLoader.

public static Class forName(String className)

Another method is to obtain the class, which is the getClass () method of each type of instance.

        System.out.println(Class.forName("java.lang.String"));        System.out.println(new String().getClass());

When the Java Virtual Machine loads a type, it uses a class loader
Locate the appropriate class file. The class loader reads in the class
File-a linear stream of binary data-and passes it to the virtual
Machine. The virtual machine extracts information about the type from
The binary data and stores the information in the method area. Memory
For class (static) variables declared in the class is also taken from
The method area


Use an instance to describe the JVM action:

class Lava {    private int speed = 5; // 5 kilometers per hour    void flow() {    }}public class Volcano {    public static void main(String[] args) {        Lava lava = new Lava();        lava.flow();    }}

Run the program. JVM finds and reads the Volcano. class file, then reads the Volcano class information in the introduced Volcano. class file, and puts the information in Method Area. Then, JVM calls the main Method by interpreting the codeByte in Method Area.
Note that when the JVM executes main (), it does not load Lava. class, And it loads as needed.
When executing main (), JVM calls enough memory space to give CostantPool the first Volcano class, and then finds the reference symbol of Lava class in ConstantPool of Volcano (that is, the character "Lava "), it checks whether Lava is loaded. When the JVM finds no, it will find and read Lava. class file, and then from the binary Lava. class, and put it in MethodArea. then, JVM will replace the first pointer (pointer to Volcano) of the ConstantPool with the pointer to the Lava class data in the method area, and then use this pointer to quickly access Lava.
Next, JVM uses the first pointer of ConstantPool (pointing to Lava information of MethodArea) to quickly obtain Lava information and allocate enough memory as needed.

(JVM always uses the MethodArea type information to determine the memory size to be called.) When JVM determines the memory size, it allocates the memory on the stack and initializes the speed variable of this object to 0. then, the reference of the generated Lava object is pushed into the stack.

Lava lava = new Lava (); the Code is complete.

Then, call flow ().

Data Type

JVM performs computation on multiple types of data.
The value of the basic type and the reference type point to the object. (The difference between the basic type and the reference type is very simple)

Boolean is mentioned here. Although it is also defined as a basic type, the java compiler compiles the source code into bytecode and loads it in JVM. boolean is represented by int or byte, 0 indicates false; a non-0 integer indicates true.

Heap

Heap is used to store instantiated objects or arrays.The heap memory is allocated. That is, the object itself (memory) is created on heap.
Two Java applications cannot share the Heap. When different threads of a java application can share the Heap, we use an object synchronously in multiple threads.

In addition, you can decide when to create an object in the code, but you cannot decide when to destroy the object (release the memory ). This is completely decided by JVM (GC manages heap, in addition to releasing space, it also moves objects to reduce memory fragments ).

We already know that the stack stores the reference of objects in the execution heap. In addition, the object in the heap retains the reference of an execution method area (for the sake of quick access type information ).

Two heap implementations:

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.