Deep understanding of Java Virtual Machine-learning notes and deep understanding of Java Virtual Machine

Source: Internet
Author: User

Deep understanding of Java Virtual Machine-learning notes and deep understanding of Java Virtual Machine

JVM Memory Model and partition

JVM memory is divided:

1.Method Area: A thread-shared area that stores data such as class information, constants, static variables, and Code Compiled by the real-time compiler loaded by virtual machines.

2.Heap:The thread-shared area, the storage object instance, and the memory area allocated to the array are also located here.

3.Virtual Machine Stack:In the thread isolation area, each thread has its own virtual machine stack with the same lifecycle as the thread. Memory Model of Virtual Machine stack description method execution, in the unit of station stack frame, each stack frame stores local variable tables, operand stacks, dynamic links, method return addresses, and other information related to method running.

4.Program counter:In the thread isolation area, each thread has its own program counter, and the row number of the bytecode currently executed by the stored program.

5.Local method Stack:Thread isolation, similar to the Virtual Machine stack, is used when the virtual machine calls the Native method.

 

Heap partition and features of each partition:

Java heap is the main area for managing the Garbage Collector. The heap memory space can be further subdivided according to the generation collection algorithm.Young Generation, old age. The young generation can be divided into largerEdenTwo zones of the same sizeFrom external vor, To external vor. The default ratio of Eden to VOR isThis ratio can be adjusted. When allocating memory for the newly created object, the object is first allocated to the Eden and From other vor areas. When the object is recycled immediately, objects in the Eden and vor regions that are still alive will be copied To the same vor region. If the size of the objects To be alive cannot accommodate the surviving objects, they will be allocated To the elderly area. In general, newly created small objects will be placed in the young generation. Most of the objects in the young generation will be recycled during the next garbage collection. The old generation will store large objects and long-lived objects.

 

Object creation method, Object Memory layout, Object Access location

Object creation:

1. the process of creating a common object: When the VM encounters a new instruction, it first checks whether the parameter (class type) of this instruction can locate the symbolic reference of a class in the constant pool, check that the class referenced by this symbol has been loaded, parsed, and initialized. If the class loading process is not executed.

2. Create an array object: When a virtual machine encounters a newarray bytecode command, a zone is directly allocated to the memory.

3. class Object creation: When a virtual machine loads a Class, it obtains the binary byte stream of this Class through the full qualified name of the Class, after file verification, the static structure represented by the byte stream is converted to the runtime data structure of the method area, and a Class object representing the Class is generated in the memory, which exists in the method area, as the entry to access various data of this class.

 

Object Memory layout:

The object layout in the memory is divided into three areas:Object Header, instance data, and padding

Object Header: Stores data in the running hours of an object, including hash data, GC age division, lock status identifier, lock held by the thread, thread ID, timestamp, etc; another part of the object header is a type pointer, that is, the pointer of the object to its class metadata in the method area. The VM uses this pointer to determine the instance of the class of the object. If the object is an array, the object header also has a syntax to record the data of the array length.

Instance data: Valid information that objects actually store. It is the content of various types of fields defined in the class.

Alignment Filling: The virtual machine requires that the object size must be an integer multiple of 8 bytes. Alignment filling plays a placeholder role to ensure that the object size is an integer multiple of 8 bytes.

 

Object Access and positioning: Java programs use the reference data on the stack to operate on specific objects in the stack. There are two access methods for objects:Handle access, direct pointer access.

Handle access: The Java heap divides a region as the handle pool and references the handle address of the stored object. The handle actually contains the specific address information of the object instance data and object type data.

Direct pointer access: The reference in the stack directly points to the address of the object in the heap, and the object points to the address of its class metadata in the Method Area in the header data.

The advantage of using a handle is that it stores a stable handle address in reference. when an object is moved (garbage collection leads to object movement), it only changes the instance Data Pointer with equal emphasis. Direct pointer access is faster.

 

Method for Determining garbage collection:Reference counting method, reference Chain Method

Reference COUNTING METHOD: Add a reference counter to an object. Add 1 to an object reference counter, and subtract 1 from a reference failure counter. If the counter is 0, the object is no longer used and can be recycled.

Reference Chain Method (Accessibility Analysis ):Using GC Roots as the starting point, it is proved that an object is unavailable when it is connected to GC Roots without any reference chain.

The objects that can be used as GC Roots are the objects referenced in the Virtual Machine stack, the objects referenced in the local method stack, and objects referenced by class static attributes in the method area, objects referenced by constants in the method area (Execution context and global reference)

 

Four reference types and features of Java:

1.Strong reference:It is common in programs. references such as "String s =" hello wold "will not recycle strongly referenced objects.

2.Soft reference: Useful but non-essential objects will be reclaimed for soft referenced objects before the system is about to experience memory overflow. SoftReference class implements soft reference.

3.Weak reference: Non-essential objects. objects associated with weak references can only survive until the next garbage collection occurs.

4.Virtual Reference: The Weakest reference relationship. Instances of objects cannot be obtained through virtual references. The only purpose of setting virtual references for objects is to receive a system notification when the object is recycled by the Collector.

The four types of reference intensity are weakened in turn, and they are weak and weak.

 

Principles, features, purposes, and optimization ideas of three GC Collection Algorithms

Three garbage collection algorithms:Copy algorithms, tag-clear algorithms, and tag-Sort Algorithms

Mark-clear Algorithm: Mark all objects to be recycled. After marking, all marked objects are recycled.Disadvantages: The process of marking and clearing is not efficient. After marking is clear, space fragments are generated. As a result, space fragments may be cleared in advance when large objects are allocated.

Copy Algorithm: The available memory is divided into two areas, each with only one of them. When the memory used is used up, the surviving objects are copied to the other memory, then, the used memory space is cleared once. Advantages: the solution is simple to implement.Disadvantages: Reduce the memory to two blocks, and the memory usage is not high. Replication is frequently performed with lower efficiency.

Tag-Sorting Algorithm: After the object tag can be recycled, move all the surviving objects to one end, and then clear the memory outside the boundary. Advantage: no space fragments are generated, which improves the memory space utilization compared with the replication algorithm.

The replication algorithm is used in the garbage collection of the young generation, and the tag sorting and tag clearing algorithms are used in the garbage collection of the old generation.

 

What are GC collectors? Features of CMS and G1 collectors

The GC collector varies according to the recycling area,The new generation includes Serial, Parnew, Paralell Scanvage, Serial Old, CMS, Parallel old, and G1;

Serial and Serial oldIt is a garbage collector released in the early jdk. It features a single thread. The new generation uses the replication algorithm and the old generation uses the mark sorting algorithm, when the two garbage collectors work, they must stop all user threads until the collection is complete before returning to the user thread. Because they work in a single thread mode, there is no thread interaction overhead, so the highest single-thread collection efficiency can be active, using a virtual machine in client mode.

ParNewThe collector is a multi-threaded version of the Serial collector and a garbage collector of the young generation. It can be used with the collectors of Serial old and CMS old. The performance of Parnew in a single CPU environment is not as good as Serial, because multithreading in a single CPU environment is executed serially in chronological order, and it also bears the additional overhead of Inter-thread interaction. However, in a multi-cpu environment, the performance of Parnew will be much better, and it is the preferred new generation collector for virtual machines running in server mode.

In jdk1.4, the new garbage collector isParallel Scanvage and the corresponding Parallel Old,The new generation is based on the replication algorithm, and the old generation is based on the tag sorting algorithm. Parallel Scanvage is also a Parallel multi-thread collector. The difference between Parnew and Parnew is that they have different concerns. Parnew focuses on the user thread pause time when garbage collection is reduced, while Parllel Scanvage focuses on getting the maximum throughput, that is, the ratio of the CPU running user code to the total CPU consumption time. Short pause time is suitable for programs that interact with users. high throughput can efficiently use CPU time to complete computing tasks as soon as possible, mainly because it does not require too many interaction tasks for background computing.

JDK introduces the ability to run concurrently with user threads.CMS collector, CMS is the garbage collector in the old age. CMS is a collector designed to obtain the minimum recovery pause time. It is implemented based on the mark clearing algorithm. Its work process is dividedInitial tag, concurrent tag, re-tag, and concurrent cleanupIn the four steps, the initial mark and re-mark need to pause the user thread, and the concurrent mark and concurrent clean-up process can be executed concurrently with the user thread, during the overall garbage collection time, the initial marking and remarking take a short time. The remarking phase can be executed by multiple garbage collection threads in parallel, so the overall user thread pause time is very short.Disadvantages of CMS: Sensitive to CPU resources. By default, the number of garbage collection threads started by CMS is (number of CPUs + 3)/4. In the concurrent stage, the application slows down due to the occupation of user threads, when there are less than four CPUs, the user program will be greatly affected. CMS cannot process the new garbage generated in the concurrent cleaning phase and can only be cleared after the next garbage collection mark; the CMS generates space fragments based on the tag clearing algorithm. The CMS solution is to enable memory sorting during Full GC. This process cannot be concurrent, which prolongs the pause time of user threads.

G1 collectorJdk1.7 is a new generation and old garbage collector for the server mode. G1 divides the memory area into multiple independent regions with the same size. G1 tracks the value of spam accumulation in each region and maintains a priority list in the background. Each time based on the allowable collection time, priority is given to the region with the highest value. This collection policy can achieve the highest collection efficiency within a limited period of time. G1 garbage collection process: initial mark (single thread, pause), concurrent mark (single thread, concurrent), final mark (multi-thread, parallel, pause), filtering and recycling (multi-thread, parallel, pause ).

 

When does Minor GC and Full GC occur respectively?

It is started once when the memory allocated to the created object is insufficient.Minor GCTo collect the new generation of Eden and From other vor areas, and allocate the surviving objects To the same vor area. If the space in the To different vor area is insufficient To accommodate the surviving objects, will allocate the surviving objects to the old age. If the old age does not have enough space, it will start once.Full GC.

 

Class loading process: Loading, verification, preparation, parsing, initialization

The Class loading mechanism of virtual machines is to load the data of the description Class from the Class file (or other channels) to the memory, and verify, convert, parse, and initialize the data, this eventually forms a Java type that can be directly used by virtual machines.

Load: 1. get the binary byte stream defining this class using the full qualified name of a Class 2. Convert the static structure of the byte stream Dai Xiao into the runtime data structure of the Method Area 3. Generate a Data Structure in the memory class Object that represents this Class, as the portal for accessing various data of this class in the method area.

Verify: 1. Verify the file format to ensure that the format of the input byte stream complies with the format specification of the Class file, and ensure that the input byte stream can be correctly parsed. Only through this verification, the byte stream is stored in the method area. 2. Metadata verification. semantic verification is performed on the class metadata to ensure that the class description information complies with the Java language specification. For example, whether the verification class implements methods in the parent class or interface, such as 3. bytecode verification, through the analysis of data flow and control flow, to ensure that the class method conforms to the logic, it will not cause harm to the virtual machine at runtime. 4. symbol reference verification occurs in the parsing phase to ensure that the symbolic reference is converted to a normal execution that is consumed directly.

Preparation: Officially allocate memory for the class variable (static) and set the class variable Initial Value (zero value of the Data Type). These variables are allocated in the method area.

Analysis: The Virtual Machine converts the symbol reference in the constant pool into drinking directly, the parsing action mainly applies to class or interface, field, class method, interface method, method type, method handle, and call point qualifier.

Initialization: Java code defined in the class is actually executed in the initialization phase. The initialization phase is the process of executing the class constructor <init> method. <Init> methods (class constructor) are generated by the compiler automatically collecting static variables in the class and merging static code blocks. The initialization priority of subclass and parent class is parent class constructor> subclass class constructor> parent class object constructor> subclass object constructor. Static class variables and static code blocks in the class are executed in the sequence defined in the class.

 

When Will class initialization be performed?

JVM specifies that only five conditions exist --Actively reference a class, Class initialization must be performed immediately.

1) when the new, putstatic, getstatic, and invokespecial bytecode commands are executed, initialize the class immediately if class initialization is not performed. The environment corresponding to these four bytecode commands is: instantiate an object using the new keyword, read or set static variables of the class, and call static methods of the class.

2) When you use the java. lang. reflect package to reflect the class, initialize the class immediately if it is not initialized.

3) When initializing a class, if the parent class is not initialized, initialize the parent class first.

4) when the VM is started, the main class of the main method will be initialized by the VM first.

5) use the Dynamic Language in lava. lang. invoke. the final parsing result of the MethodHandle instance is the method handle of REF_getdtatic, REF_putStatic, REF_invokeStatic. The class corresponding to this handle is not initialized and needs to be triggered first.

 

Parental Delegation Model:

The Parent-parent Delegation Model of the Class Loader refersStarting from the top layer to the bottom layer are the class loaders, extended class loaders, application class loaders, and custom class loaders.. The parent-child relationship between class loaders is not implemented through inheritance, but through combination.The working process of the parent-child delegation model is: If a Class Loader receives a class loading request, it first delegates the request to the parent class loader to complete the process, all the load requests should eventually be sent to the top-level startup Class Loader. Only when the parent class reports that it cannot complete the class loading request will the auto-loader try to load the requests by itself.

Benefits of using a parent-child Delegation Model: A java class has a level of priority with its loader. The most basic class is loaded by the class loader at the top layer, this ensures that the same class is used in the place where the class is used in the program.

 

Related Article

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.