Go [Reading notes] deep understanding of Java virtual machines

Source: Internet
Author: User
Tags field table instance method scalar stack trace

Original Address: http://blog.csdn.net/hanekawa/article/details/51972259

Chapter II Java memory area and memory overflow exception

One, run-time data region:

1. Program counter: The line number indicator of the byte code executed by the current thread, the bytecode interpreter works by changing the value of this counter to select the byte code instruction that the next hop needs to execute. If you are performing the native method, the value of this counter is empty.

2. Java Virtual machine stack

The thread is private and the life cycle is the same as the thread. Describes the memory model that the Java method executes: Each method creates a stack frame to store information such as local variable tables, operand stacks, dynamic links, method exits, and so on. Each method from the call to the completion of the process, corresponding to a stack frame in the virtual machine stack into the stack of the process.

The local variable table holds various basic types of data, object references, returnaddress types known to the compiler

3. Local Method Stack

Provide virtual machine stack services to the native method used by the virtual machine

4. Java Heap

A piece of memory that is shared by all threads, created when the virtual machine is started, and holds the object instance. Almost all object instances are allocated memory here. is the main area of garbage collector management.

5. Method area

An area of memory shared by each thread that stores the class information, constants, static variables, immediate compiler-compiled code, and so on, that have been loaded by the virtual machine. Also called the permanent generation

6. Run a constant-rate pool

Part of the method area that holds the various literal and symbolic references generated by the compiler

7. Direct Memory

NIO directly allocated out-of-heap memory using the native library

Second, the object creation process

Virtual opportunity to a new directive, first checks whether the parameter of this directive can be positioned in a constant pool to a class's symbolic reference, and check whether the symbol reference represents the class has been loaded parsing initialized. If not, you must first perform the appropriate class loading process. After the class load check passes, the virtual machine then allocates memory to the heap for the new object. After the allocation is complete, the virtual machine needs to initialize the allocated memory space to a value of 0. Next, the virtual machines set the object header for the object, write an instance of which class it is, how to find the metadata information of the class, the hash code of the object, the age of the GC, and so on. From the point of view of the virtual machine, the new object has been completed, but the Java program also needs to do initialization work,<init> method execution.

Third, the object's access location

1. Access by handle


2. Access by direct pointer


Chapter III garbage Collector and memory allocation policy

An algorithm for judging the life and death of an object

1, reference counting method

Add a reference counter to the object whenever there is a place to reference him when counter +1, when the reference fails counter-1; Any object with counter 0 at any time is impossible to be used again. The disadvantage is that it is difficult to solve the problem of circular references between objects.

2, the Accessibility analysis algorithm

Through a series of GC root objects as the starting point, starting from these nodes to search down the path of the search as a reference chain, when an object to the GC roots no reference chain connected, this object is not available, recyclable. Gc Roots has the following objects: objects referenced in the virtual machine stack (local variable table), objects referenced by class static properties in the method area, objects referenced by constants in the method area, objects referenced by JNI in the local method stack.

Two, reference type: Strong reference, soft reference, weak reference, virtual reference.

Strong references: Ubiquitous presence in program code

Soft references: Describes some objects that are also useful but not necessary. The system will list the objects in the collection scope for a second recycle before a memory overflow exception occurs.

Weak references: Used to describe non-required objects, weaker than soft references, objects associated with weak references can only survive until the next garbage collection occurs.

Virtual reference: The weakest reference relationship, where the sole purpose of setting a virtual reference for an object is to receive a system notification when the object is reclaimed by the collector.

The Finalize () method fires after the object is first tagged and is considered necessary by the virtual machine, and the object will be placed in the fqueue and triggered by a later finalizer thread (not guaranteed to execute). To perform some pre-collection operations. is executed only once.

Four, garbage collection algorithm

1, Mark-Clear algorithm: First mark out all the objects that need to be recycled, and collect all the tagged objects uniformly after the tag is complete. Cons: The efficiency of marking and clearing two processes is not high; After a mark clears, it leaves a large amount of discontinuous memory fragmentation, causing the program to allocate large objects without finding enough contiguous memory to trigger another garbage collection in advance

2, copy algorithm: Divide available memory by capacity into two blocks of equal size, using only one piece at a time. When this one has run out of Jiujiang, the surviving object is copied to the other piece, and the used memory space is erased once.

3, Tag-collation algorithm: The tagging process is just like the tag cleanup algorithm, so that all surviving objects are moved to one end, and then the memory outside the end boundary is cleared.

4, generation of collection algorithm

Five, garbage collector

1, serial Collector

2, parnew Collector

3, Parallel scavenge collector

4, Serial old collector

5, CMS Collector: The collector to obtain the shortest recovery time for the target. Based on the tag cleanup algorithm, it is based on four steps:

A) The initial mark (stop) marks the object that the GC roots can directly relate to

b) Concurrent tagging for GC roots tracing

c) re-tagging (stop) fixes a tag record for a part of an object that is causing a change in markup during the operation of a user program during a concurrent tag

D) Concurrent cleanup

Cons: Very sensitive to CPU resources

Unable to process floating garbage

The collection ends up with a lot of space debris.

6, G1 Collector: divides the entire Java heap into separate areas of equal size.

Characteristics:

A) parallelism and concurrency

b) Collection of generations

c) Space Integration

D) Predictable pauses

The G1 collector can establish a predictable pause-time model because he can plan to avoid full-area garbage collection throughout the Java heap. G1 maintains a prioritized list based on the size of the garbage accumulation in each area, giving priority to reclaim the most valuable areas per collection time.

Steps:

A) initial mark (stop)

b) Concurrent tagging

c) Final mark (stop)

d) filter recovery (stop)

Six, memory classification and recovery strategy

1, object precedence in Eden area assignment

2, large objects go straight into the old age

3, the long-term survival of the object will enter the old age

4, dynamic object Age determination (if the sum of all objects of the same age in the survivor space is greater than half of the survivor space, objects older than or equal to that age can enter the old age directly)

5, Space allocation guarantee

Fourth. Virtual machine performance monitoring and fault handling tools

One, JPS: Virtual Machine Process Health tool

Second, Jstat: Virtual Machine Statistics monitoring tool

Three, Jinfo:java configuration information tool

Four, Jmap:java memory Image tool

Five, Jhat: Virtual machine heap dump snapshot analysis tool

Six, Jstack:java stack trace tool

Seven, Hsdis:jit generate code Disassembly

Eight, Jconsole:java monitoring and management console

Nine, VisualVM: Multi-in-a-fault processing tool

Chapter Sixth class file structure

One, class file structure

1. Magic number and class file version

2. Constant pool: Two major types of constants: literals (constants, such as literal strings, constant values declared final), and symbolic references (fully qualified names of classes and interfaces, field names and descriptors, name and descriptor of a method)

3. Is the access flag (class or interface, public type?) Abstract Final )

4. Class index, parent class index, interface index collection: Used to determine the inheritance relationship of a class

5. Field table collection: Used to describe an interface or a variable declared in a class. Fields include class-level variables and instance-level variables, and do not include local variables declared inside a method.

Fully qualified name: Org/sun/clazz/testcase;

Simple name: method or field name without type and parameter Adornments Inc ():-> Inc

Descriptor: Describes the data type of the field, the parameter list of the method, and the return value

The Field table collection does not list fields inherited from the superclass or parent interface, but it is possible to list fields that do not exist in the original Java code.

6. Method table collection: Includes access flags, name indexes, descriptor indexes, property sheet collections

In the Java language, to overload a method, in addition to having the same simple name as the original method, requires that you have a signature that differs from the original method. Signature is a collection of field symbol references for each parameter in a method in a constant pool, that is, because the return value is not included in the signature, so in the Java language it is not possible to overload an existing method simply by relying on the difference in the return value. However, in the class file format, feature signatures are larger in scope, as long as the descriptor is not exactly the same two methods can coexist. That is, if two methods have the same name and signature, but the return value is different, it can also legitimately coexist in a class file.

7. Property sheet Collection

Seventh chapter virtual machine class loading mechanism

The virtual machine loads the data of the description class from the class file into memory, verifies and initializes the data, and finally forms the Java type which can be used directly by the virtual machine, which is the class loading mechanism of the virtual machine.

two, class load life cycle : Load, validate, prepare, parse, initialize, use, unload.

Three, there are only five cases where the class must be initialized immediately:

1. When encountering the 4 bytecode directives of new, getstatic, Putstatic, invokestatic.

2. When a reflection call is made to a class using the Java.lang.reflect package method.

3. When initializing a class, the parent class needs to be initialized if its parent class has not yet been initialized.

4. When the virtual machine starts, the user needs to specify a main class (main ()) virtual opportunity to be executed to initialize the main class first

5. When using jdk1.7 's dynamic language support, if the final parse result of a Java.lang.invoke.MethodHandle instance ref_getstaticref_putstatic the Ref_invokestatic method handle, And this method handle has not been initialized for the corresponding class, you need to first trigger its initialization

Four, class loading process

1. Loading

A) get a binary byte stream that defines this class by using the fully qualified name of a class

b) Convert the static storage structure represented by this byte stream into the runtime data structure of the method area

c) Generate a Java.lang.Class object representing this class in memory as the access entry for various data of this class in the method area

2. Verification

A

3. Preparation: The prep phase is exactly the stage in which memory is allocated for class variables and the initial values of class variables are set, and the memory used by these variables is allocated in the method area.

A) memory allocations include only class variables (variables that are modified by static), and do not include instance variables.

b) The initial value is typically a 0 value of the data type (public static int val=123, val=0 after the prep phase, assigning Val to 123 is performed during the initialization phase of the program after it is compiled, stored in the class constructor <clinit> () method)

4. Parsing

A) symbol reference: A set of symbols to describe the referenced target, the symbol can be a class and form of the literal, as long as the use can be ambiguous to target, regardless of the memory layout implemented by the virtual machine, the referenced target is not necessarily loaded into memory.

b) Direct reference: Can be a pointer directly to the target, a relative offset, or a handle that can be indirectly positioned to the target. The direct reference is related to the memory layout implemented by the virtual machine, and the referenced target must already exist in memory.

5. Initialization: The process of executing the class constructor <clinit> () method

A) <clinit> () is generated by the assignment of all class variables in the compiler's auto-collection class and the statements in the static statement block, and the order of the compiler phones is determined by the order in which the statements appear in the source file. A static statement block can only access variables defined before a static statement block, and variables defined in the preceding static statement block may be assigned values but cannot be accessed

b) The <clinit> () method differs from the constructor of a class in that it does not require a display to call the parent class constructor, and the virtual opportunity guarantees that the parent class's <clinit> () is executed before the subclass <clinit> () method executes. The first one to be executed <clinit> () is definitely java.lang.Object

c) the <clinit> () of the parent class is executed first, meaning that the static statement block defined in the parent class is preceded by the variable assignment operation of the child class

D) the <clinit> () method is not necessary for classes and interfaces, and if there is no static statement block in a class and no assignment to the variable, the compiler may not generate <clinit> () for this class

e) Interfaces cannot use static statement blocks, but still have assignment operations for variable initialization. The <clinit> () of the execution interface does not need to perform the <clinit> () of the parent interface first. The implementation class of the interface does not execute the interface's <clinit> () method at initialization time

f) Virtual opportunities ensure that a class of <clinit> () is correctly locking and synchronized in a multithreaded environment.

Five, class loader

1. For any class, it is necessary to establish its uniqueness in the Java Virtual machine, together with the class loader that loads it and the class itself. Compare two classes are equal, only if these two classes are loaded by the same classloader, it makes sense, or even if the two classes from the same class file, by the same virtual machine home, as long as the home in their class loader different, then the two classes must be unequal.

2. Parental delegation Model

A) There are only two kinds of loaders for virtual machines: the Launcher ClassLoader (Bootstrap ClassLoader, implemented in the C + + language, is part of the virtual machine itself), and all other class loaders (Java authoring is independent of the virtual machine outside, All inherited from Java.lang.ClassLoader)

b) There are three types of loaders for developers: Launch class loader (<java_home>\lib), extension ClassLoader (<java_home>\lib\ext), application loader (default ClassLoader in program, ClassPath)


3. Parental delegation Model Workflow: If a class loader receives a request for a class load, he first does not attempt to load the class himself, and 28 the request is delegated to the parent ClassLoader to complete, as is the case for each level of the ClassLoader. The child loader tries to load itself only when the parent loader has feedback that it cannot complete the load request.

Eighth chapter virtual Machine bytecode execution engine

One, run-time stack frame structure

1. The stack frame stores the local variable table of the method, the operand stack, the dynamic link, and the method return address. Each method from the start of the call to the completion of the process, all corresponding to a stack frame in the virtual machine stack from the stack to the process.

2. Local Variables table: is a set of variable value storage space for the method parameters and methods internally defined local variables. The virtual machine uses the local variable table in the way that the index locates.

3. Operand stack: is a post-in first out stack. When a method is just beginning to execute, the operand of this method is empty, during the execution of the method, there will be various bytecode instructions to write to the operand stack and extract the content, that is, into the stack and out of the stack.

4. Dynamic linking: Each stack frame contains a reference to the method that the stack frame belongs to in the run-time pool, and this reference is held to support dynamic connections during method invocation. A part of a symbol reference that exists in a constant pool of a class file is converted to a direct reference when the class is loaded or when it is first used, called Static parsing, and the other part is converted to a direct reference during each run, which is called a dynamic connection.

5. Method return Address

A) When a method begins execution, there are only two exit methods: The execution engine encounters the bytecode instruction returned by either method, which is called normal completion exit, and the other exit method is an exception encountered during the execution of the method, and the exception is not processed in the method body. As long as the exception table in this method does not search for a matching processor, it causes an exception to exit, which becomes an exception completion exit. Regardless of the exit method, the program must return to the location where the method was called after the method exits, before the procedure can continue execution.

b) The process of exiting the method is actually equivalent to putting the current stack frame out of the stack, so the possible actions to exit are: Restore the local variable table of the upper method and the operand stack, press the return value into the operand stack of the caller stack frame, and adjust the value of the PC counter to point to an instruction following the method invocation instruction.

6. Additional Information

Two, method invocation

1. Allocation: Divided into static allocation and dynamic allocation.

A) static dispatch: The virtual machine is determined by the static type of the parameter instead of the actual type when overloading the overload, and the static type is known as the compile time, so the compiler will determine which overloaded version to use based on the static type of the parameter in the compile phase javac. All dispatch actions that rely on static types to locate a method's execution version are called static allocations. A typical application method for static dispatch is method overloading. Static dispatch occurs during compilation, so determining the statically dispatched action is not actually performed by the virtual machine.

b) Dynamic dispatch: There is a close association with the polymorphism override override.

The invokevirtual instruction runtime parsing process is broadly divided into:

c) Find the actual type of the object that the first element of the top of the operand stack points to, C; If a method is found in type C that matches both the descriptor and the simple name in the constant, the access check is performed, and if passed returns the direct reference to the method, the lookup process ends; returns error without permission If no matching method is found, then the parent class of C is searched and verified according to the inheritance relationship, and then the error is thrown if the method is never found.

d) The parameters of the method's recipients and methods are collectively referred to as the method's volume. Depending on how many kinds of parcels the allocation is based on, the allocation can be divided into single dispatch and multiple dispatch. A single dispatch is a selection of the target method based on one volume. Multi-allocation is the choice of the target method based on the extra volume.

The static assignment of the Java language belongs to the multi-dispatch type, and the dynamic assignment belongs to the single dispatch type.

e) Use virtual method table indexes instead of metadata lookups to improve performance. The virtual method table holds the actual entry address of each method, if a method is not overridden in a subclass, the address entry in the virtual method table of the subclass and the address entry for the same method as the parent class are consistent, pointing to Fred's implementation entry; If this method is overridden in a subclass, The address in the subclass method table will be replaced with the entry address that points to the subclass implementation version.

11th chapter Operation Period optimization

One, optimization technology

1. Common subexpression elimination: If an expression e has been calculated, and from the previous calculation to the current E in all the variable values have not changed, then the appearance of E is a common subexpression, only need to directly use the previously computed expression results instead of e can be

2. Array boundary Check elimination: If the compiler can determine that the value range of the loop variable is never out of bounds as long as it is analyzed by the data stream, the upper and lower bounds of the array can be eliminated in the entire loop

3. Method Inline: "Copy" the Code of the target method into the method that initiates the call, avoiding the actual method invocation. Because only private methods, instance constructors, parent methods, and static methods are parsed at compile time, other Java method calls require a polymorphic selection of method receivers at run time, that is, the default instance method in the Java language is a virtual method. Without solving this problem, the CHA type Inheritance Relationship analysis technique was introduced to determine whether there are more than one implementation of an interface in a class that is currently loaded, whether a class has subclasses, whether subclasses are abstract classes, etc.

4. Escape analysis: The basic behavior is to analyze the object dynamic scope. He may be referenced by external methods, such as calling parameters to other methods, called method escapes, and possibly even accessed by external threads, called thread escapes.

If you can prove that an object does not escape to a method or thread, you can perform some efficient optimizations for that variable:

I. Stack allocation: allocating memory on the stack for in-thread objects

II. Synchronous elimination: Synchronization measures implemented on this variable can eliminate

Iii. scalar substitution: If a Java object is torn apart, it is called a scalar substitution to restore the original type of the member variable to which it is accessed according to the program's access.

Two, Java vs. C + + compiler : the Java Virtual machine's instant compiler has the following disadvantages compared to the static optimization compiler

1. Because the instant compiler runs on the user's program run time, has a lot of time pressure, he can provide the optimization method is also severely constrained by the cost of compiling

2. The Java language is a dynamic type-safe language, which means that a virtual machine is required to ensure that the program does not violate linguistic semantics or to access unstructured memory, which means that the virtual machine must perform frequent dynamic checks.

3. Because the Java language does not have the virtual keyword, but the frequency of using virtual methods is far greater than C + +, the instant compiler is much more difficult to optimize (inline) than C + +

4. The Java language is a dynamically expanding language, and loading new classes at run time can change the inheritance of program types, making it difficult to make many global optimizations.

5. The memory allocations for objects in the Java language are on the heap, only local variables in the method can be allocated on the stack, and C + + has multiple allocations, and if a thread-private object is allocated on the stack, the memory-reclaim pressure is mitigated. In addition, C + + in the main by the user program code to reclaim allocated memory, run-time efficiency than the garbage collection mechanism is higher.

These performance weaknesses of the Java language are the cost of developing efficiency benefits, dynamic security, dynamic scaling, and garbage collection contributing to the development efficiency of the Java language.

12th Java memory model and threading

One, the Java memory model


Two, the antecedent principle : is a partial order relationship between two operations defined in the Java memory model, if operation a precedes operation B actually means that the effect of operation a before the operation B can be observed by Operation B.

Three, thread state transitions

Go [Reading notes] deep understanding of Java virtual machines

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.