Deep understanding of Java Virtual Machine-Reading Notes

Source: Internet
Author: User

Deep understanding of Java Virtual Machine-JVM advanced features and best practices

Author: Zhou Zhiming

Post-reading: I don't need to say much about this book. Many people recommend this book. After reading this book, I also feel very good. I strongly recommend this book as a java-related friend. The following are my reading notes. I have noted down some of my important points. Most of them are summaries and some are self-organized languages. If there is anything wrong, please correct me.

1. jvm memory structure 1) program counter
A smaller memory area can be seen as the row number indicator of the bytecode executed by the current thread; Each thread is independent;
2) Java Virtual Machine Stack
Describes the Memory Model of java method execution, that is, each method creates a stack frame during execution. Users can store information such as the local variable table, operand stack, dynamic link, and method exit; that is, the process of calling a method until it is completed corresponds to the process of putting a stack frame into the stack to go out of the stack. It is also a thread private;
3) Local method Stack
The native method used by the corresponding virtual machine, similar to the Java Virtual Machine stack;
4) java heap
When a virtual machine is created at startup, almost all object instances and data must be allocated on the heap (except for jit, escape analysis, and other optimization technologies). The size can be expanded, through-Xmx and-Xms parameter control; thread sharing;
5) Method Area
Stores information about classes loaded by virtual machines, constants, static variables, and Code Compiled by the real-time compiler;
6) runtime frequent pool
It can be seen as a part of the method area to store various literal and symbolic references generated during the compilation period (not necessarily only the compilation period ).
7) Direct Memory
It can be seen as off-heap memory, not limited by the java heap size;

The above classification is easy to understand by myself: Stack (Virtual Machine stack and local room development), stack, Method Area (permanent generation ?), Program counters, direct memory;
2. gc collection 1) Collection algorithm:
Quote technical algorithms: Add 1 for reference, and subtract 1 for reference failure. It is difficult to solve circular references between objects;
Accessibility analysis algorithms (mainstream java Implementation): This object is unavailable when an object is connected to GC Roots without any reference links;
Mark-clearing algorithm: the basis of all algorithms, which are divided into two stages: "mark" and "clear". Low efficiency leads to a large number of discontinuous memory fragments;
Replication algorithm: divides the memory into two equal parts by capacity, each of which uses only one. When this part is used up, it copies the remaining objects to the other part, and then clears the used memory space. Simple implementation, efficient operation, and a waste of space;
Tag Sorting Algorithm: The tag process is still the same as the "tag-clear" algorithm, but the subsequent steps do not directly clean the recyclable objects, but move all the surviving objects to one end, then, the memory outside the end boundary is cleared directly;
Generational collection algorithms: (currently used for garbage collection by commercial virtual machines) are generally divided into the new generation and the old generation. There are only a small number of surviving objects in the new generation, which are collected using the replication algorithm, the survival rate is high. You can use the "mark-clean" or "mark-clear" algorithms to recycle them.

Recycling Method Area: This area is also called permanent generation, with low cost performance, reflection, dynamic proxy, and dynamic jsp generation;

2) Memory Allocation
In general, objects are allocated on the stack (but may also be split into scalar types after JIT compilation and indirectly allocated on the stack). objects are mainly distributed in the Eden area of the new generation, in a few cases, the allocation rules may not necessarily be in the old age, depending on which Garbage Collector and Vm-related parameters.
3. class loading mechanism 1) loading time, lifecycle: loading --> Verification --> preparation --> resolution (this step is inconsistent in sequence) --> initialization --> Use --> uninstall
Initialization scenario: 4 bytecode for new, getstatic, putstatic, and invokestatic; java. lang. reflect package for reflection call; initialize the parent class before initializing a class; execute the main method class; When jdk1.7 is a dynamic language, the handle is REF_getStatic, REF_putStatic, REF_invokeStaticde;
2) class loading process (the following definition is the truncation summary)
Load: Get the binary byte stream of the Class, convert the static storage structure to the data structure of the runtime in the method area, and generate a java. lang. Class Object that represents this type in the memory;
Verification: ensures Virtual Machine security, file format verification, metadata verification, bytecode verification, and symbol reference verification
Preparation: the phase in which the class variables are allocated memory and the initial class variables are set. The memory used by these variables will be allocated in the method area.
Resolution: The Virtual Machine replaces the symbolic reference in the constant pool with the direct reference process,
Initialization: Execution class Constructor () Process of the method (change the method to thread security ). Contains static variables and number of constructed rows
3) Class Loader
Knowledge Point: judge whether two classes are equal (including equals, isAssignableFrom (), isInstance (), and instatceof ), only when these two classes are loaded by the same class loader makes sense.
Parent-Child Delegate model: Start the Class Loader <-- extended Class Loader <-- Application Class Loader <-- custom Class Loader
Working Process: If a Class Loader receives a class loading request, it first delegates the request to the parent class loader instead of trying to load the class by itself, this is true for class loaders at each layer. Therefore, all loading requests should be finally transmitted to the top-level startup class loaders, only when the parent classloader reports that it is unable to complete the load request (the required class is not found in the search range) Will the child loader attempt to load it by itself.
See illustration
Destroy parent-parent delegate model: 1) Thread Context loader (Theard Context ClassLoader), java. lang. Thread class setContextClassLoader (); 2) Hot loading, OSGi
4. class compilation *. convert the java file *. class file process; back-end compilation converts bytecode into machine code (JIT); or static early compiler directly converts *. the process of compiling local machine codes for java files.
Syntactic sugar: Generic and type erasure, automatic packing, unpacking and traversal loops, and Conditional compilation;

(Late) HotSpot built-in 2 in-time Compilation: Client Compiler and Server Compiler (client mode and server Mode)
The hotspot code is mainly compiled here. There are two main methods for hotspot detection: Sampling-based hotspot detection (stack top method) and counter-based hotspot detection;
Compiler optimization technologies include:
1) one of the classic language-independent optimization technologies: Elimination of common subexpressions;
2) one of the classical language-related optimization technologies: array range check and elimination;
3) One of the most important optimization technologies: Method inline; (simply understand how to copy the code of the target method to the method that initiates the call, so that a real call occurs; complicated process :))
4) One of the most cutting-edge optimization technologies: Escape analysis (JDK1.6)
Method escape: when an object is defined in a method, it may be referenced by an external method. For example, it is passed as a call parameter to another method;
Thread escape: access by external threads, such as copying to class variables or instance variables that can be accessed in other threads;
If an object does not escape, it can be highly optimized, such as stack allocation, synchronization elimination, and scalar replacement (Object disassembly );
5. Concurrency 5.1 java Memory Model
Master memory and working memory: the java memory model requires that all variables are stored in the master memory, and each thread has its own working thread, the worker memory of a thread stores copies of the primary memory used by the thread. All operations (read and assign values) on the variable by the thread must be performed in the worker memory, instead, you cannot directly read or write the variables in the primary memory.

5.2 inter-memory interactive operations: the java Memory Model defines eight atomic operations. The jvm must report that each operation is an atomic operation,
Lock, (lock, the variable acting on the main memory)
Unlock, (unlock, the variable acting on the main memory)
Read (read, acting on the main memory ),
Load (load, put into the working memory ),
Use (variables acting on the working memory ),
Assign (assign value to work memory ),
Store (storage, variables acting on the working memory );
Write (write, the variable acting on the main memory)
[Knowledge point] If you want to copy a variable from the main content to the working memory, read and load operations should be performed sequentially. If you want to synchronize the variable from the working memory to the main memory, store and write operations must be performed sequentially. Note that the java memory model only requires that the preceding two operations be executed in sequence without the guarantee of continuous execution.
Rules that must be met in the above 8 operations:
1) read and load operations are not allowed.
2) A thread is not allowed to discard the latest assign operation.
3) a thread is not allowed to synchronize data from the working memory of the thread to the main memory for no reason.
4) A new variable can only be "generated" in the main memory and cannot be used directly in the working memory.
5) Only one thread can lock a variable at the same time, but the lock operation can be repeatedly executed multiple times by the same thread, the variable is unlocked only when the unlock operation is performed for the same number of times;
6) If you perform the lock operation on a variable, the value of this variable in the working memory will be cleared. Before the execution engine uses this variable, you need to re-execute the load or assign operation to initialize the variable value;
7) if a variable is not locked by the lock operation in advance, the unlock operation cannot be performed on it.
8) before you execute unlock on a variable, you must synchronize the variable to the main memory (execute store, write );

5.3 volatile
Volatile-lightweight synchronization mechanism provided by Java virtual machine. Two important features. 1 is to ensure that this variable is visible to all threads, 2 is to disable command re-sorting optimization,
1) Refresh Every time before use, and the execution engine cannot see any inconsistency to ensure visibility; however, the volatile variable does not have consistency issues in the working memory of each thread (or there may be inconsistencies), but the operations in java are not atomic operations, as a result, the computation of the volatile variable is not safe under the same concurrency. (The java Memory Model specifies that the load and use actions are continuous, and the store and write actions are continuous)
2) command rescheduling in terms of hardware, the Command Re-sorting means that the CPU allows the development of multiple commands not in the order specified by the program to be sent to the corresponding circuit unit for processing. However, this does not mean that the CPU needs to be able to correctly handle the dependency of commands to ensure that the program can produce correct execution results.
Performance: the performance consumption of volatile read operations is almost the same as that of common variables, but write operations may be slower, because he needs to insert many memory blocking commands in the Local Code to ensure that the processor does not execute in disorder.
Security:
Synchronization is still required in the following scenarios:
1) The calculation result does not depend on the current value of the variable, or it can ensure that only a single thread can modify the value of the variable;
2) variables do not need to participate in the constant constraint with other state variables;

5.4 long and double
Non-atomic protocols, 64-bit data types, load, store, read, and write operations do not guarantee atomicity. (Currently, almost all commercial jvm implementations use 64-bit data read/write operations as atomic operations)

5.5 atomicity, visibility, and orderliness (conclusion)
Atomicity: read, load, assign, use, write
Visibility: the java Memory Model synchronizes the new value to the primary memory after the variable is modified. The variable value is refreshed from the primary memory before the variable is read. This mode relies on the primary memory as the transmission medium for visibility. Volatile, synchronized, final (except for escape by this reference ;)
Orderliness: If observed in the local thread, all operations are ordered; If observed in one thread, all operations are disordered;

Escape analysis: When a variable (or object) is allocated in a method, its pointer may be returned or referenced globally, which will be referenced by other processes or threads, this phenomenon is called the Escape of a pointer (or reference ). (From the Internet)

5.6 current principles
This refers to the partial order relationship between the two operations defined in the java memory model. If operation A first occurs in operation B, it means that before operation B occurs, the impact of Operation A can be observed by Operation B. "impact" includes modifying the value of shared variables in the memory, sending messages, and calling methods.
The "natural" first occurrence relationship in the java Memory Model:
1) program order rules: a thread and Code Order (Control Flow order );
2) process lock rules:
3) volatile variable rules: write operations first occur in subsequent read Operations
4) thread startup rule: the start () method first takes place in every action of this thread;
5) thread termination rules: join
6) thread interruption rules:
7) object termination rules; Initialization prior to finalize ();
8) transmission;

Thread status:
1) Create new
2) Run runable
3) waiting for waiting without deadline
4) waiting for timed waiting
5) block blocked
6) end terminated
6. thread security the "security level" of thread security is sorted as weak as possible:
1) immutability, final
2) Absolute thread security (mark itself as a thread-safe class in java APIs, most of which are not absolute thread security)
3) Relative thread security: This ensures the thread security when the object is operated independently. No additional safeguard measures are required during the call. However, for some sequential calls, you may need to use additional Synchronization Methods on the caller to ensure the call is correct. Eg: Vector, HashTable;
4) thread compatibility. the object itself is not thread-safe, but it can be used correctly by the caller to ensure that the object can be safely used in the concurrent environment; eg: arraylist and hashMap;
5) opposition to threads

Implementation of thread security:
1) mutex synchronization (blocking synchronization): mutex is the cause, synchronization is the result, mutex is the method, synchronization is the purpose;
Java. util. concurrent. ReentranLock adds some advanced features than synchron: Waiting For interruptions, implementing fair locks, and binding multiple conditions;
2) non-blocking synchronization: (generally speaking, it means continuous retry and knowing the success). Optimistic Concurrency policies require support from the hardware Instruction Set;
Common instruction sets:
Test and Set Test-and-Set
Get and add Fetch-and-Incremet
Swap Switching
Compare and exchange CAS Compare-and-Swap (vulnerability ABA problem)
Load connection/Conditional Storage Load-Linked/Store-Conditional
3) No synchronization solution: code re-entry, Thread Local Storage (ThreadLocal)

Lock Optimization
1) spin lock and adaptive selection (cas)
2) Lock elimination (based on escape analysis), String class, String addition, before JDK1.5 converted to StringBuffer class (thread safety); JDK and later, StringBuilder
3) the locks are roughened and the scope is expanded.
4) Lightweight lock, jDK1.6 added,
5) biased lock

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.