java entire understanding

jvm memory model

  • Heap
  • Methods district
  • Program Counter
  • Java Virtual Machine stack
  • Native method stacks
  • Runtime constant pool
  • Direct Memory
    ****

    Heap

    This is the biggest piece of jvm in the region, substantially all of the variables and objects are instances in which a storage area, shared by all threads. But with the release of jdk1.6 optimization, JIT compiler technology of real-time, escape analysis technology matures, all objects allocated on the heap is not so absolute, so long as the object does not escape analysis or escape from the thread stack, then the original in variables allocated on the heap can be directly allocated on the stack, so the method call ends, or the end of the thread, automatically release the memory on the stack, can greatly reduce the GC to collect the heap memory, improve overall operating efficiency jvm. OutOfMemeryError will throw an error heap jvm life cycle and life cycle of the same, with the start of the JVM to generate, terminate the JVM released
    ****

    Methods district

    This part is shared by all threads. The method area code is stored in the type of information, constant, variable and static real-time compiler, also known as permanent methods district on behalf of, or related information are stored here, as well as character variables and references. OutOfMemeryError error will be thrown, life cycle and the same JVM
    ****

    Java Virtual Machine stack

    This section is the thread private, each thread will have a virtual machine stack, the virtual machine stack is the memory model Java method execution, every method in the virtual machine stack corresponds to a stack frame, the stack frame is stored in, the method requires the use of calls to a local variable table (8 large basic data types and the reference object reference), the parameter information of the method, a dynamic link, the operand stack, the method outlet. Java is not c, c ++, and other related links course in the class file, there is no real memory addresses directly to the entrance of the variables, but the address of the symbol referenced in class loading and parsing time, will convert the symbolic references to variables memory real memory entry address. Dynamic Link: This is the stack frame in their respective area of reference methods, the method area is stored in class information. Operand stack: Java execution process is explained based on the execution stack, so that execution of a method, the parameter stack the stack, the bytecode instructions stored in the operation on the operand stack. It will throw OutOfStackFlowError and OutOfMemeryError wrong, life cycle and the same thread
    ****

    Native method stacks

    Stack, some virtual machine when performing a local methods, such as hotspot virtual machine directly to the virtual machine stack and native method stacks into one, and also the thread private. It will throw OutOfMemeryError and OutOfStackFlowError error. Life cycle and the same thread
    ****

    Program Counter

    Thread private, belonging to each thread, the main role, because the realization of Java multi-threaded rely thread switch turns to get the CPU execution time, to determine the time of the same, only one thread of instructions is executed CPU, a cpu only one thread can execute instructions, and for multi-core CPU, the same time, only one core can execute one thread of instructions. Recovery thread, abnormal jump, perform branching, looping, bytecode instructions are required to the program counter, all things thread private, not shared, life cycle and the same thread. Life cycle and the same thread
    ****

    Direct Memory

    Jvm memory does not belong to the management, JavaNIO need to use an external JVM memory when memory space directly to the operating system application, when the application fails, will throw an error OutOfMemeryError
    ****

    Runtime constant pool

    This part belongs to the area of ​​Java method, also shared by the threads, because the whole method area shared by all threads. Where version information is stored in the class, field information, the information method, and also direct reference symbol references (later referenced class object class loading, after loading class as symbolic references resolved converted into direct reference)

    In the process of creating the Java objects jvm

When jvm receive a new instruction when the method will first go find the corresponding area of ​​parameter (category) This instruction symbolic reference exists and that is loaded jvm, if not loaded, it will trigger class loading process. When the class is loaded later, the object need to use the memory size is also determined, memory space is allocated for the object in two ways, the first pointer collision (the same block size to the object can be moved), a second species are free list, with take garbage collection algorithms and related collector. Very frequent when the object is created in a virtual machine, because Java itself is a pure object-oriented language, in the case of high concurrency, it may appear to the object A is allocated memory space when the pointer is not enough time to modify, then thread B and use the pointer to allocate memory situation. Java There are two ways to solve this case, one is accompanied by CAS failure retry way to ensure atomic update of memory allocation operations. Another local thread allocation buffer (previously allocated a small memory in each thread), i.e. allocate memory in the thread, TLAB allocated on that thread when the thread TLAB after use, only need genlock can -XX: +/- UseTLAB to set parameters. After the memory allocation is completed, memory initialization virtual machine needs to assign a value of zero, to ensure that the object instance field may not be assigned an initial value in the Java code to be used directly, can access to a zero value of the class variable. Next, the virtual object confidential necessary settings, such as the example object which belongs to a class, how to find the class meta data, hash code value of the object, the GC generational age and other information, which is stored in the object header in. After the work is completed, from the point of view of virtual machines, a new target was produced, but from a Java perspective, this object is new and has not been executed Method, class or variable value of zero, the new instruction is then executed in general execution A method according to initialize the object value meaning programmers, such an object is actually considered completely available emerge.
****

Memory layout objects

Subjects were divided into three parts, the first objects, instance data, filling it with

  • Object head: the object itself runtime data, such as hash code (HashCode), GC generational age, lock flag, thread holds the lock, bias timestamp.
    Another part of the object is the head pointer to the type, i.e., a pointer to its class object is metadata, the virtual machine to determine the current object belongs to the class of that instance through the pointer.
  • Part of the sample data: real significance information stored in the object, the information field is used in the program to the class, according to the programmer Real data after initialization method, comprising inherited from parent classes down, or in the subclass definition, it will be recorded.

Access and positioning objects

  • Handle manner: the local variable table in the Java virtual machine stack pointer stored in the reference object located in the pool handle to handle stack address pool allocated memory space. Pool handle to an object instance is stored in the address data, and address points to metadata object classes
  • Pointer: Address is the Java virtual machine heap objects stack local variable table stored in the reference pointer to the object stored in the instance data and the metadata information of the object class.
    ****

Object reference (after jdk1.2)

  • Strong references: new out, jvm willing to throw enough memory, it will not remove references to the object of this part
  • Soft Quote: before the second garbage collection to survive
  • Weak references: not required, can survive the first garbage collection before
  • False quote: doing anything useful, just in time for garbage collection, to effect a notification.
    ****

How to determine the objects in the heap can be recycled

There are two ways, the first counter is an object references another object, then the counter + 1, reference off, the -1 when the counter is 0, indicating that the object is not referenced, and dependence , then that can be cleared, although this method can be achieved, also easy to use, but is determined not recognize circular dependency issue, so there will be two cycles dependent objects, not useful, but it will not be the system recycling. The second is, reachability analysis, namely a series of objects called virtual opportunity to GCROOT taken as the starting point, to start the search down from these nodes traversed path is called the chain of references, when an object has no references when connected to the chain, i.e. the object is not reachable GCROOT to prove that this object is not available, can be recovered. GCROOT generally have a Java virtual machine stack object in the local variable table referenced in the static properties of the modified method area object reference, object methods zone constant references, JNI native method stack referenced object. The real target will be cleared through two phases, the first phase, the object is marked as can be cleared when calls finalize () method, this method will only be called once, then the object will not be cleared, but will F-queue is placed into the queue, low priority thread queue will be automatically created by a virtual machine at a later time to do it. If the queue objects can be external and in any case a reference object associated chain, then it will not be recovered. The recovery efficiency of the method area is lower, because the area is also known as permanent methods district, a direct reference to the future of storage classes are loaded or symbolic references, class objects, constants and static variables. These objects can survive for a long time, to determine whether the object class can be recycled, it needs to meet three points, a class does not have any instance of an object, the second class loader loads the class was recovered, java three class. lang.class object is not referenced in any place, in any place that is not reflected by access to methods of the class. In this case, the class is useless, it can be recycled.

Garbage collection algorithm

  • Mark - Clear: Clear marking and is divided into two processes, the two process efficiency is not high, and this algorithm will generate a lot of discrete space, leading to later when large objects requires a large space, may no longer trigger a garbage collection but simple.
  • Mark - Finishing: and mark - sweep the same principle, at different points, is not clear, but the survival of the object to the side, so you can clean out the space directly outside the boundary.
  • Replication algorithm: the memory space is divided into two, each of which only one, which one needs to carry out garbage collection, clean-up of the space to be first surviving objects move to another piece of space-time, and then once the current space of clean up. Drawback is that only half of the available space, the cost is too high, modern virtual machine to improve the algorithm, the memory space is not 1: 1, but is divided into 1: 9, to be exact, Eden: 80%, the remaining 20% 10% of the survivor is divided into two zones, just use one of Eden and Survivor areas when needed garbage collection, which also survived the moving object to the rest of the region to a survivor, then clean out the space. If the object can not be installed at the time survivor survival appears, this time the need to ensure years old, which is the guarantee mechanism.
  • Generational collection algorithm: divided into the old era, the young generation. The new generation of objects in the survival rate is not high, you can use the copy algorithm years old objects because of the high survival rate, long time, you can use the mark - sweep or mark - Collation Algorithm recovery.
    ****

    HotSpot algorithm

  • Enumeration root: Now many applications only method area have hundreds of megabytes, if there is individually checked references, will inevitably consume a lot of time and reachability analysis of sensitive execution time is also reflected in GC pauses, namely stop the world, as cited in the analysis chain, for accuracy, the whole chain of references can not be changed, which led to the GC is carried out, must stop all Java execution threads, GC will not happen even in the so-called pause CMS collection vessel, enumerate the root node, you also have to pause. Current mainstream Java virtual machine is accurate formula GC, so when execution system to a standstill and does not need to be left out of all references checked and global execution context, but the use of a data structure to achieve this OopMap purpose.
  • Safety point: with the help of OopMap, HotSpot can quickly enumerate the root node, but it raises the question, OopMap content changes very much, if instructions are generated for each corresponding OopMap, it will need a lot of extra space, so that the cost becomes high GC. What is the security point, is the message OopMap record. To this end, there are two good ways to solve the problem of security points, steals type of formula, one is active. Steals formula occurs in the GC, first of all the threads break, if we find where there is not a security thread interrupts point, then resume the thread, it went to the point of safety, now basically no virtual machine using a steals type thread in response to pause GC events, is active when the GC needs to be interrupted thread, the thread does not directly operate, but simply set a flag when each thread execution initiative to poll this flag, the interrupt flag is found true, on their own interrupt pending.
  • Regional Security: Security point to solve the efficiency problem of enumeration of the root node, the point must be security thread is active (ready or runing) If the thread is block or wait, can not respond jvm thread interrupt request, to secure the point to interrupt the hanging since, this time to introduce a secure area to solve this problem, a piece of code, a reference relationship does not change, GC anywhere in this area are safe and can be seen as the SafeRegion was expanded in SafePoint.


The garbage collector

  • Serial Collector: single-threaded default collector under the Client mode
  • ParNew Collector: multithreaded version of Serial
  • Parallel Scavenge collector: the new generation of collectors using the copy algorithm
  • Serial Old collectors: Serial old collector's collector
  • Collect old's version of Parallel Scavenge collector: Parallel Old collectors
  • CMS collector: the shortest collection pause collector, web applications commonly used
  • G1 Collector: Preface current results of the garbage collector, but not perfect, continuous improvement in the

Java Memory Model

  • Working memory: Java all operations on variables, are carried out in the working memory.
  • Main memory: variables and instance objects are born in the main memory.

In the working memory of the thread, and the thread between the respective non-collinear transparent. How to implement Java variables visible in each thread it through the main memory, each thread in this thread copy of the value of the variable in the main memory has been modified or after the operation, the main memory will be synchronized by the Java Memory see operating agreement . To achieve visibility of variables between threads in such a way.

Java memory between (working memory and main memory) Operating Agreement (comparative physical computer to solve the MESI cache coherency protocol)

  1. lock: The main memory is identified as a variable thread exclusive state
  2. read: the main memory to read a variable thread's working memory
  3. load: reading from the main memory to write a copy of the variable in the variable working memory
  4. use: the value of the variable passed to the execution engine to use, when the virtual opportunity to bytecode instructions require the use of a variable time, it will perform a
  5. assgin: the return value from the execution engines are stored in working memory when the virtual opportunity to require assignment to a variable when it will be performed once
  6. store: the value of working memory variables passed back to the main memory
  7. write: write launched back from working memory variable value is written back to main memory variables go
  8. unlock: The main memory is identified as a thread to unlock exclusive state variables, variables can be unlocked only after the other thread locked

8 or more operations are atomic, no longer divided, not because thread scheduling is interrupted and switched
to the upper side corresponding to operating protocols 8, Shihai JVM executes the predetermined operation must meet the following eight rules of operation

  • read, load and store, write must be paired, that initiated the request from the main memory to read variable values, and working memory does not accept or initiate a request to write back from the working memory, and main memory does not accept the situation.
  • Thread can not discard recent assgin operation, that is worthy of variable changes must be written back to main memory
  • It does not allow a thread reads a variable value, but without any modification (assgin operation) will write back to main memory
  • The same time a variable can only be one thread locked, but can be several times a thread lock must be unlocked same number of times when the lock, other threads can use.
  • Variables can not go to unlock locked by another thread
  • a lock main memory variables, you must clear the copy of the value currently locked thread in the working memory of this variable, then re-read the value of this variable from main memory
  • A copy of the value of the variable in the main memory when unlock a variable, you must first unlock operation initiated by the current thread's working memory is written back to main memory
  • A variable can only be born in main memory, are not allowed in the working memory in a thread not initialized (assigned initial value) of the variable. This is why local variables must be initialized.

The above operation agreement between memory and must be met eight operating rules, we can determine in the case of high concurrency, those memory operations are thread-safe. But this to judge, troublesome procedure is cumbersome, so there is a principle equivalents, (happens-before) to simplify this principle occurs first determination process

(Happens-before) the principle of first occurrence

  • Program sequence rules: a thread, a thread is noted, i.e., the single-threaded mode, occurs in front of the code preceding the code behind, of course, needs to be determined according to flow control.
  • Tube-lock rule: it is the same lock, unlock lock operation occurred in the first operation, remember that it is the same lock.
  • volatile rule: write to a volatile variable is modified to read the first occurrence of this variable.
  • Thread start rule: start Thead object () method first occurrence of this all the actions of the thread.
  • Thread break the rules: interrupt thread method calls (interrupt ()) first occurrence in this thread detects an interrupt occurs.
  • Thread the end of the rule: all methods in the thread first occurrence in this thread stop () method, which first occurred in the termination detection.
  • End of rule objects: init object () method first occurrence of this object's finalize () method.
  • Transitivity rule: A first operation occurs in Procedure B, B ahead operation occurs in Procedure C, then A occurs first in C.

volatile keyword

  1. Variable Visibility: operation of the modified variables to be volatile, are synchronized in time back to main memory, without delay, just as you do the same variables in main memory.
  2. Prohibit instruction reordering: When the modified volatile variable operation, JIT compiler optimization of real-time, can not put volatile variables have to be placed behind the execution, that is the variable in what position, then it will be in this position before or it is executed in this position, not because the instruction reordering to back execution, all dependent changes worthy, was able to get the right result, if the modification was not operating atomic variables have the volatile, there will be a variable value expires The problem. Prohibition instruction reordered bytecode is added to a memory barrier operation, after which the barrier to invalidate the Cache cpu, which led to a new re-read, load process, to achieve visibility this way.

Visibility in Java, atomicity, ordering

  1. Atom: the basic data types in Java operations are atomic, atomic, ie can not be divided, not because of a thread switch and stop, cpu must complete to perform an atomic operation, in order to make the implementation of cpu time to perform other bytecode instructions in the thread. Java to achieve atomicity keyword is synchronized.
  2. Visibility: the revision of the value of the variable memory to instantly synchronize back to main memory. Java synchronized, volatile keyword implemented by final,.
  3. Orderliness: the performance of serial, multiple threads are disordered in a single thread, instruction reordering (scrambled physical computer optimization) as well as working memory and main memory synchronization delay caused.

Java thread-safe under high concurrency

In the case of high concurrency, how to achieve thread-safe, jvm have their way. Jvm in Java or in multi-thread by switching threads in turn acquires the CPU execution time to complete, at the same time the CPU can have a bytecode instruction in one thread can be executed cpu. Thread-safe definition: Generally speaking, an object regardless of what way to call, no matter what order to call, regardless of multiple threads to call, or unsafe situations dirty data will not occur, then we say this object is thread-safe in a multithreaded.

  • Immutable class: Immutable classes are naturally thread-safe, an immutable class or all of the state variables are final modified, then when an object of this class is created correctly, we can not in change, then this object is the thread safe. Class specific implementation can refer Java.lang.String and enumerated classes, subclasses, and achieve some of Number.
  • Stateless categories: stateless object class, which means that only the behavior of the object (method) data from all operating parameters are passed over, at this time this variable is encapsulated in the thread, or Java Virtual Machine stack is transparent to other threads, other threads can not access this variable, naturally, thread safety problem does not occur, then also thread-safe.
  • Synchronous blocking: is achieved by the synchronized keyword or ReentrantLock, Synchronized only modification methods, classes and fields can not be modified. Synchronized bottom layer is achieved by a bytecode monitorenter and monitorexit two instructions, a native level jvm support, JavaApi of ReentrantLock is implemented by the bottom spin and CAS + park + blocking queue implementation. Synchronous blocking principle is, entering access to shared resources critical areas need to get to the lock object, synchronized key modification of the object, that is, lock the object, if not specified, then look modification is an instance of an object or class object, if it is an example objects on instances can correspond to the contrary is get a class object, and only get to the lock to enter the critical section, after the end of access to shared resources protected and leave the critical region, it is necessary to return the lock object, other threads can compete acquire the lock.
  • Optimistic CAS: need hardware support, after jdk1.5, with the development of the hardware instruction set, retry mechanism can fail to complete the operation atomic access to shared resources.
  • Thread localization: Thradlocal, database connection pooling, producer-consumer queue, web applications (corresponding to a request to a server thread) and the like.
  • Reentrant code is: That does not access the class variable in a method that can pause code execution does not affect the results of subsequent recovery, the same parameters can be passed as a result of, recursive, pure method.
    ****

Lock optimization

  1. Adaptive spin and spin: The creation and scheduling with Java threads about creating Java threads depends on the operating system, use the thread scheduling is preemptive. Switching threads need to switch in kernel mode (kernel) and user mode back and forth, this is a waste of time and performance, in order to solve this occurred, the current thread is not really made a CPU execution time, but the first spin several times, if after the spin several times, still not hot, then it really makes the CPU execution time. Adaptive spin the principle is similar, except that the number of spins it.
  2. Lock elimination: The escape analysis techniques, JIT when optimizing found that variable will not escape the current thread, or will not escape the current method, the method of locking lock can be removed, because the lock is very time-consuming, larger impact on performance .
  3. Coarse-grained locks: lock e.g. in one cycle, are optimized to JIT, locking the entire method, instead of locking the loop.
  4. Lightweight lock: synchronized belongs to the heavyweights lock, easy NA, lightweight lock, also called optimistic locking CAS, was intended to reduce the traditional heavyweights locked in a competition under the premise of no use multithreading operating system mutex generated performance loss. Remember, lightweight heavyweight lock does not replace the lock. This operation relies lock flag in the object header.
  5. Biased lock: synchronization primitives in a non-competitive situation, in the absence of competitive situation, even the CAS can be removed.

Java class file structure

  1. Cube number
  2. Minor version number
  3. The major version number
  4. Constant pool: all kinds of symbolic references
  5. Access qualifier
  6. Class Index
  7. Parent index
  8. Interface index set
  9. Fields Collection
  10. Collection method
  11. Property sheet collection

Java class loading process

  1. Load: Get defined by the fully qualified name of the class of such binary byte stream, the byte stream that represents the static storage structure into a run-time data structure area method, generates a representative of the class in memory java.lang.Class objects, as various methods of data access region of the inlet of this class.
  2. Verify: verification file format, bytecode verification, metadata validation, verification of symbolic references.
  3. Preparation: initialize static class field value of zero, if no static field, may not generate clinit method of class
  4. Analysis: The constant pool reference symbols process of converting direct reference, Java is not connected in step c, c ++ language and the like, class files are not stored in memory layout information, dynamically partially resolved when the class is loaded. Class resolution, field analytical, analytical methods class, interface method resolution, static methods class constructor, the parent class methods, private methods is certain, not because Java polymorphic and there are different versions of the call, it can be classloading analytical phase is completed analytical methods, instance methods objects that need to perform in order to determine the true version at the time of the call.
  5. Initialization: This step is really to initialize the object in accordance with the will of the programmer's steps.
  6. Use: Use an object, complete functions.
  7. Uninstall: After use in the future, recycled object, class unloading.

Java method invocation

Object-oriented three characteristics: encapsulation, inheritance, polymorphism. Java is a pure object-oriented language, naturally supports multi-state, multi-performance Java: method overloading (overload), the method override (override), assigned Java methods can be divided into static and dynamic, according to the number of cases assigned different amount, Java methods can be divided into single and multiple dispatch dispatch, ie, combinations of two static single assignment, multiple dispatch static, dynamic single assignment, dynamic multiple dispatch. Static assignment: The method of signature that method (method parameter types, order, method name, etc.) and a method to determine the recipient of the call, the dynamic assignment: When override the parent class method, jvm when calling a method, based on the Java Virtual Machine real data type stack object to determine methods or subclass call the parent class.
****

Early optimization

Compile-time optimization

  • javac compiler: parsing the symbol table and fill
  • Annotation Processor
  • Semantic analysis to generate bytecode
  • Java syntactic sugar: type erasure in generics, automatic packing, unpacking in through the loop, conditional compilation

Late optimization

Optimized runtime

  • Common subexpression elimination, array bounds checking elimination, within the joint optimization method, escape analysis

Guess you like

Origin www.cnblogs.com/yjp372928571/p/12214974.html