Depth study focuses on java-based --- Chapter One: In-depth understanding jvm (java virtual machine) Section gc java memory model and strategy

As a java programmer I do not know if will use the principle called junior java programmer, its principles and mid-level rise, compared with high-level mastery

As there is a technical pursuit of the people, we should spare time and understand the principle of fragmented time

Recently in-depth understanding of java virtual machine to see the second edition (based on jdk1.7) so wanted to write after a number of concept ideas, organize some of the more focused the content, but also strengthen their memories of key content!

 

The following default virtual machine to virtual machine hotsport

A .jvm memory model

① Program Counter:

  The program counter for each thread private

    Effect may be understood as the line number recorded in byte code parsing virtual machine bytecode

    A small amount of memory

    Is the only area of ​​a memory overflow will not occur (OOM) of


 

② virtual machine stack:

 Virtual machine thread stack is private

 VM stack includes a java virtual machine stack and native method stacks, the only difference between the two is that the former is for java latter is for a local method (native method can be understood as a method operating system)

 A stack the stack frame includes a local variable in the method, various information for export, operand stack, dynamic link, etc.

   Note that the stack stored only basic data types and reference types variables, memory allocation objects are in the heap

    Push to stack the stack frame for each thread invokes a method corresponding to the method execution ends

 The depth of the stack is dynamically extensible

 When the depth is greater than the depth of the virtual machine to allow the calling thread (adjust virtual machine stack memory use xss parameter) occurs StackOverflowError (SOF) abnormal

 Each stack frame stack push determines whether sufficient memory capacity, will be found to be insufficient memory for memory i.e., when not enough memory to apply OutOfMemoryError (OOM) exception occurs

 Because the virtual machine stack strict compliance with the method call stack method has finished executing the rules of the stack, so no recovery gc


 

③ heap

 java main stack area gc (Garbage Collecte11d) recovered

 java heap is shared by all threads

 java stockpiling stood almost all object instances, it only kept the object instance

   Heap into the young generation, old generation

   The young generation is subdivided into Eden and two survivor zone area, the area gc recovery algorithm for replication algorithm, on gc algorithm will be mentioned below

 The young generation are stored newborn near objects (gc has not been recovered or recycled after gc little use MaxTenuringThreshold parameter settings gc after several objects can be promoted to the old generation)

 Old generation memory for storage of some of the larger large object (using PertenureSizeThreshold parameter setting this memory limit value) or gc still alive after several objects

   When another case, when the sum of all objects Survivor area greater than one half the size of the same age Survivor Chief memory, the object is greater than or equal to the age old generation promotion all

 Storing large objects because the old generation and a longer survival period characteristic of the object, so the use of mark - Collation Algorithm

 When an object is instantiated to the heap if the heap can not provide a continuous memory space will trigger gc, gc and after still can not allocate enough memory space OOM exception occurs


 

④ method area

 The method area is shared by all threads

 The method area stores class information have been loaded, the static class variables, constants and other data

 The method area contains the runtime constant pool, including the compilation of literals and generates various symbolic references, and the runtime constant pool can dynamically increase run time data in the process

 Sometimes the method area and also the young generation and the old generation of years and called permanent, but the area is not permanent in fact method, the method area also recovered gc

 When the object is constant pool does not exist any reference will be recovered gc

 When no instance exists a class, the class loading classLoader has been recovered, the corresponding java.lang.Class object is not referenced in any place, not by the method of accessing the class reflected anywhere, can be recovered

 The same method when the district can not meet the OOM exception occurs when a memory allocation needs

Two .gc strategies and algorithms gc

 In the virtual machine program counter and stack stack frame stack because the stability of the stack does not need too much to consider gc problem, this is mainly for java heap memory gc explaining to do

 We only need three points clear of you can understand the process gc

    1. What needs to be recovered memory

 2. When Recycling

 3. How to recycle

 

 What needs to be recovered memory: of course, do not need to reuse objects only need to be recycled, how to determine the object has been useless? There are two algorithms determines one reference counting algorithm, the second is reachability analysis algorithm, a calculation algorithm is to calculate reference number references between various objects java uses the latter, so that the former is not much to do here described

 

 Reachability analysis algorithm: using a series of objects as gc roots is the root node to traverse the entire chain of references, references to objects outside the chain of objects with neither

 

 Gc roots object as follows:

    java virtual machine stack referenced objects

    The method of the object in a static variable references

    The method of constant reference object region

    Local virtual machine stack referenced objects


 

 It should be noted that reference this concept, cited references divided into four, four, referral ordering from strong to weak as follows:

    Strong references - out of all New objects are strong references, references to objects on the chain as long as there is a strong reference even OOM exception occurred can not be recovered gc

    Soft references - out of memory when the system is about to take place, will give priority to the recovery off the soft references

    Weak references - the direct recovery occurs when a weak reference gc

    False quote - no effect, can not even get a virtual reference to the object instance, the only effect is when the object is recovered will receive a notification system


 

 When recovery: In most cases objects are allocated in the Eden area, and when the lack of memory will trigger the Eden District Minor Gc

   So the higher the distribution rate, the more frequently performed Minor GC, Minor Gc can be understood as only the young generation were Gc recovery strategy.

   When the young generation Survivor space is insufficient, it occurs Full Gc Gc can be understood as the recovery of all of the heap and the old generation of the remaining space can not bear to Surivivor

   Undernourishment Full Gc's old space when a large object allocation

   For loading class constant pool or zone in the process of dynamically increasing the constant region of space shortage occurred method Full Gc


 

 How to recover:

   Three common gc algorithms: mark - sweep, copy, finishing mark

 

 Mark - sweep - the first mark all available objects, and then clear out all non-available objects

 

 Copy - divided into A, B two memory space, there will be all the data in the A region, gc occurs when copying objects to available space B, and a clear all the memory space A, the next copying gc is repeated B A further clearing the memory space B

 

 Mark - Consolidation - mark all available objects, and then move to the end of all available objects, and then removed the disposable outer end of the non-available object boundary

 

 Combined with the young generation and the old generation algorithm for analysis of each area gc

 

 The young generation - because most of the young generation Eden area objects are "raw evening toward death" so the use of mark - sweep or mark - finishing efficiency will be low, and the use of high and very efficient algorithm to copy a waste of space, so the young generation improvements the replication algorithm, using the Eden area and two Survivor areas, because once gc Eden District surviving objects are usually very small, so the young generation defaults to 8: 1: 1 ratio for memory allocation area of Eden and two Survivor areas, gc occurs when the survival of the Eden area and a target area in survival Survivor object is copied to another area of Survivor, then clear out memory Eden area and an area of Survivor, it will ensure the efficiency gc, but not so It will waste a lot of memory space

 

 The old generation - the old generation because of longer storage life cycle of large objects or objects, so you can not use the same copy as the young generation algorithm, because that could lead to a large number of object replication, leading to low efficiency, but this time mark - collation algorithm advantage of reflected, it only needs to mark an object in a non-tenured generation of the few available after then non-available object is moved to the end of the heap can be a one-time clearance

Guess you like

Origin www.cnblogs.com/buggeerWang/p/10927178.html