The final summary of the learning notes of the deep understanding of Java virtual machines

Source: Internet
Author: User
Tags visibility

Compiler

Java is a compiled language, depending on the time of compilation, the compiler can be divided into:

    1. Front-end compiler: actually called the compiler's front-end more appropriate, it transforms the *.java file into a *.class file, such as Sun's Javac, Eclipse JDT in the incremental compiler ECJ;
    2. JIT Compiler: Virtual machine backend Runtime compiler (Just in time Compiler), which converts bytecode into machine code, such as the Hotspot VMd C1, C2 compiler;
    3. AOT compiler: Static advance compiler (Ahead of Time Compiler), which compiles *.java files directly to the cost of machine code, such as the gun Compiler for the Java (GCJ), Excelsior JET;

The front-end compiler has done many optimizations to encode the process to improve the coding style and improve the coding efficiency of the programmer, such as a lot of new Java syntax features, are relying on the front-end compiler "syntax sugar" to achieve, and the virtual machine design team to optimize the performance of the JIT compiler, This allows class files that are not generated by Javac to enjoy the benefits of compiler optimizations as well.

The Java program was originally interpreted by the interpreter (interpreter), and later, in some commercial virtual machines (Sun HotSpot, IBM J9), when the virtual machine discovers that a method or block of code is running particularly frequently, the code is identified as a "hotspot Code" (Hot Spot code) ", in order to improve the execution efficiency of hot-spot codes, the virtual machine will compile the code into the machine code associated with the local platform and perform various levels of optimization at runtime, the compiler that completes this task is called the Instant compiler (Just in time Compiler).

While not all Java virtual machines have an interpreter-and-compiler architecture, many mainstream commercial virtual machines, such as hotspot, J9, and so on, contain both interpreters and compilers: When a program needs to be started and executed quickly, the interpreter can work first, omitting the time to compile and execute immediately. When the program runs, as time goes by, the compiler becomes more efficient by compiling more and more code to compile the cost code.

The two instant compilers built into the hotspot virtual machine are client Compiler and server Compiler, or C1 and C2, respectively. However, whether the compiler used is client Compiler or server Compiler, the way the interpreter and the compiler are used in a virtual machine is called "Mixed mode (Mixed mode)", but you can force the virtual machine to run in interpreted mode by using the parameter-xint ( interpreted mode) ", at which time the compiler does not intervene at all, all the code is executed in an interpreted manner, and you can use the parameter-xcomp to force the virtual machine to run in the" compilation Mode (Compiled) ", which will take precedence over the compilation method. However, the interpreter will still be involved in the execution if the compilation is not possible.

Compilation Optimizations

Because the instant compiler compiles native code that requires program run time, the longer it takes to compile more optimized code, and the more optimized code that you want to compile, the interpreter may also want to collect performance monitoring information for the compiler, which can also affect the speed of interpreting execution. In order to achieve the best balance between program startup responsiveness and operational efficiency, the hotspot virtual machine will gradually enable a layered compilation strategy, with the concept of layered compilation appearing during the JDK1.6 period, which has been in an improvement phase, and eventually turned on as the default compilation policy in the JDK1.7 server mode virtual machine. Layered compilation divides different levels of compilation according to compiler compilation, optimization scale, and time-consuming, including:

    • The No. 0 Layer: Program interpretation execution, the interpreter does not turn on the performance monitoring function (Profiling), can trigger 1th layer compile;
    • Layer 1th: Also known as C1 compilation, the bytecode compiled into local code, for simple and reliable optimization, if necessary, will be added to the performance monitoring logic;
    • 2nd layer (or above Layer 2): Also known as C2 compilation, but also to compile bytecode into local code, but will enable some compilation time-consuming optimizations, and even based on performance monitoring information for some unreliable radical optimization;

After implementing a layered compilation, client compiler and server compiler will work together, many of the code can be compiled multiple times, using the client compiler to get a higher compilation speed, with the server compiler for better compilation quality, There is no need to assume the task of collecting performance monitoring information when interpreting execution.

There are two types of "hot code" that are compiled by the instant compiler during the run: The method that is called multiple times and the loop body that is executed more than once, but whether that is the whole method as the compilation object, because this compilation occurs during the execution of the method, so be is visually called a stack replacement (on stack REPLACEMENT,OSR).

To know if a piece of code is not hot code, it is necessary to trigger the immediate compilation, this behavior is called Hot spot detection, the main hot spot detection methods have two types:

    • Sampling-based hotspot probing: the virtual opportunity periodically checks the top of each thread's stack, and if some (or some) method is found to often appear on top of the stack, this method is the "hot spot". The advantage of hot spot detection based on sampling is simple and efficient, and the disadvantage is that the result is not accurate. (for example, a thread is blocked, the top of the stack is always method A, and the virtual machine periodically samples only detects this method a ...) )
    • Hot spot detection based on counter: the virtual opportunity of this method establishes counter for each method (even code block), counts the execution times of the statistic method, and determines that it is a "hot spot method" If the number of executions exceeds a certain threshold.

The second, counter-based hotspot detection method is used in the hotspot virtual machine, so it prepares two counters for each method: The method call counter and the back edge counter. Both counters have a certain threshold value when the virtual machine's operating parameters are determined, and JIT compilation is triggered when the counter exceeds the threshold value.

The method inline compiler optimizes important optimization methods, because it not only eliminates the cost of method calls, but more importantly, it is the basis for other optimization methods, because it is easier and more efficient to optimize by centralizing the code. If you want to see an immediate compilation, you can use the parameters:-xx:+printcompilation.

JVM Memory model

Main memory and working memory

    • All of the variables are stored in main memory
    • Each thread also has its own working memory, a copy of the object that owns the main memory
    • Threads can only manipulate their own working memory, and interactions between threads can only be communicated through the main memory

Inter-memory Interaction: The Java Memory Model defines 8 atomic operations, and the JVM reports that each operation is atomic:

    1. Lock (Locked, as a variable for main memory);
    2. Unlock (unlocked, used as a variable for main memory);
    3. Read (reads, for main memory);
    4. Load (load, put into working memory);
    5. Use (as a variable for working memory);
    6. Assign (assigned value, for working memory);
    7. Store (storage, as a variable for working memory);
    8. Write (writes, as a variable for main memory);

If you want to copy a variable from the main content to the working memory, perform the read and load operations sequentially, and if you synchronize the variables from the working memory back to the main memory, you will have to perform the store and write operations sequentially. Note that the Java memory model requires only that the two operations above must be executed sequentially, without guarantee of continuous execution.
The rules that must be met in the above 8 operations:

    1. Does not allow one of the read and Load,store and write operations to appear separately
    2. One thread is not allowed to discard his most recent assign operation
    3. Does not allow a thread to synchronize data from the thread's working memory to main memory for no reason
    4. A new variable can only be "born" in main memory, and does not allow direct use of an uninitialized variable in working memory
    5. A variable allows only one thread to lock it at the same time, but the lock operation can be repeated multiple times by the same thread, and after performing the lock multiple times, the variable will be unlocked only after performing the same number of unlock operations;
    6. If you perform a lock operation on a variable, that will empty the value of this variable in the working memory and need to re-execute the value of the load or assign operation initialization variable before the execution engine uses the variable;
    7. If a variable is not locked by the lock operation beforehand, it is not allowed to perform a unlock operation on him.
    8. Before performing a unlock on a variable, the variable must be synchronized to the main memory (execution store,write);

Volatile
Volatile--java provides a lightweight synchronization mechanism for virtual machines. 2 Important features. 1 is to ensure that this variable is visible to all threads, and 2 is to prohibit command reordering optimizations,

    • refresh before each use, the execution engine does not see inconsistencies and guarantees visibility, but volatile variables do not have a consistency problem in the working memory of each thread (there can also be inconsistencies), but the operations in Java are not atomic operations. The operation that causes the volatile variable is not as secure as it is in concurrency. (Java memory model stipulates that load and use actions are continuous, store and write actions are continuous)
    • Command rearrangement from the hardware, the command reordering refers to the CPU used to allow multiple instructions not in accordance with the program specified in the order of development to each corresponding circuit unit processing. However, it does not mean that the command is arbitrarily re-queued, the CPU needs to be able to properly handle the instruction dependencies to ensure that the program can produce the correct results.

Performance: The performance cost of volatile read operations is almost no different from normal variables, but the write operation may be slower because he needs to insert many memory masking instructions in the local code to ensure that the processor does not run out of order.

The following scenarios still need to be synchronized:

    • The result of the operation does not depend on the current value of the variable, or can guarantee that only a single thread modifies the value of the variable;
    • Variables do not need to participate in invariant constraints with other state variables;

atomicity, Visibility, and ordering (summary):

    • atomicity : Read,load,assign,use,write
    • Visibility: The Java memory model is modified to synchronize the new value back to main memory after the variable is read, and to refresh the variable value from main memory before the variable reads. This relies on main memory as a means of transmitting media to achieve visibility. Volatile,synchronized,final (except for this reference escape;)
    • Order: If you observe within a local thread, all operations are orderly, and if another thread is observed in one thread, all operations are unordered;

Escape analysis: When a variable (or object) is allocated in a method, its pointer may be returned or globally referenced, which is referred to by other procedures or threads, which is called a pointer (or reference) escape (escape). (from the Internet)

current principles of occurrence

Refers to the partial-order relationship between two operations defined in the Java memory model, and if operation a precedes operation B, it is said that the effect of operation A can be observed by Operation B before Operation B, "Impact" includes modifying the values of shared variables in memory, sending messages, calling methods, and so on.

The "natural" antecedent of the Java memory model:

    1. Program Order rules: A Thread, code order (Control flow order);
    2. Pipe Lock rule:
    3. Volatile variable rule: write operation occurs in the following read operation
    4. Thread Start rule: the Start () method precedes each action of this thread;
    5. Thread Termination rule: Join
    6. Thread Break rule:
    7. object finalization rule; initialization precedes finalize ();
    8. transitivity;

Thread state

    • Create New New
    • Run runable
    • No time to wait for waiting
    • The term waits timed waiting
    • Blocking blocked
    • End terminated

How to implement thread safety

    • Mutex synchronization (blocking synchronization): Mutual exclusion is the cause, synchronization is the fruit; mutual exclusion is the method, synchronization is the purpose
      Java.util.concurrent.ReentranLock, there are some advanced features added to the Synchron: Waiting can be interrupted, a fair lock can be achieved, and a lock may bind multiple conditions;
    • Non-blocking synchronization: ( popular is to continue to retry, until successful), optimistic concurrency strategy, requires the support of hardware instruction set;

Lock optimization

      • Spin Lock and Adaptive Choice (CAS)
      • Lock elimination (based on Escape analysis), String class, string addition, JDK1.5 before conversion to StringBuffer class (thread safe); JDK1.5 and later, StringBuilder
      • Lock coarsening, Extended Range
      • Lightweight lock, jDK1.6 added,
      • Biased lock

The final summary of the learning notes of the deep understanding of Java virtual machines

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.