Java's JVM Learning--Simple understanding of GC algorithms

Source: Internet
Author: User
Tags reflection xms

JVM Memory Composition Structure:

(1) heap

All objects created through new are allocated memory in the heap, and the size can be controlled by-XMX and-xms, the heap is divided into Cenozoic and old generation, and the Cenozoic is further divided into Eden and survivor area. The survivor is divided into from space and to space, and the structure is shown as follows:

(2) stack

Each thread executes each method by applying a stack frame in the stack, each containing a local variable area and a stack of operands. Used to hold temporary variables, parameters, and intermediate results during this method call

(3) Local method stack

Used to support the execution of the native method. Stores the state of each native method call

(4) Method area

Stores the class information to load, static variables, final type constants, properties, and method information. The JVM uses a durable generation (permanet generation) to store the method area, and the minimum and maximum values can be specified by-xx:permsize and-xx:maxpermsize.

(5) Program counter

The number of bytes per thread that is private and the current thread executes the bytecode.

Java Heap memory allocation mechanism

Java memory allocation and recycling in a nutshell: Generational allocation, generational recycling. The object will be divided according to the time of inventory: young generation, Old generation,permanent generation.

Yong Generation: When an object is created, the allocation of memory occurs first in the younger generation (large objects can be created directly in the old generation), and most of the objects are not used immediately after creation, so they quickly become unreachable and are young generation GC mechanism (IBM's research shows that 98% of objects are dying quickly), and this GC mechanism, called Minor GC or Young Gc;minor GC, does not represent a lack of memory.

Young generation is divided into 3 regions, Eden area, two survivor zone (from Survivor, to survivor), and the memory allocation process is as follows:

1. Most objects have just been created to be allocated in the Eden area, most of which will soon perish, and the Eden area is contiguous memory space, where memory is allocated extremely fast.

2. Initially, when the Eden area is full, execute the minor GC, clean up the extinct objects, and copy the Eden,survivor 1 remaining objects to a survival zone survivor 0 (Survivor 1 is blank at this time, Two survivor always have one is blank)

3. The next time Eden is full, once the minor GC is executed, the extinct objects are cleared, the surviving objects are copied into the Survivor1, and the Eden area is emptied. Clean up the objects that are extinct in Survivor 0, upgrade the objects that can be promoted to the old area, and copy the surviving objects to Survivor 1, emptying survivor 0

4. After being copied back and forth several times by two survival periods, (with-xx:maxtenuringthreshold control, greater than this value into the old generation, but this is only a maximum value, does not mean that it must be this value, because: in order to better adapt to the memory situation of different programs, The virtual machine does not always require that the age of the object must reach maxtenuringthreshold to be promoted to the old age, if the sum of all object sizes in survivor space is greater than half of the survivor space, Objects older than or equal to that age can go straight into the old age without waiting for the ages required in the maxtenuringthreshold. The object that is still alive will be copied to the old generation.

Old generation: If the object survives long enough in young generation and is not cleared, the space that will be copied to the older Generation,old generation is generally much larger than that of young generation, The number of GC occurrences is also less than the younger generation, when the old generation of memory is insufficient, will execute Major GC, also called full GC;

You can use the-xx:+useadaptivesizepolicy switch to control whether a dynamic control strategy is used, and dynamically adjust the size of each area in the Java heap and the age at which you enter the old age.

If the object is larger and young generation is out of space, the large object is assigned directly to the old generation (large objects may trigger the GC ahead of time, using less large objects and less short-lived large objects). Use-xx:pretenuresizethreshold to control the size of objects directly ascending into older generations, and objects larger than this value are allocated directly to the old age.

There may be cases where older generations of objects refer to a new generation of objects, and if you need to perform a young GC, you may need to query the entire old age to determine whether you can clean up the collection, which is obviously inefficient. The solution is to maintain a block of byte-"card table" in the old generation, where all the records of older objects referencing the Cenozoic objects are recorded here. Young GC, as long as check here can, no longer to check all the old age, so performance greatly improved.

JAVA different generation GC mechanism

Young Generation: The GC that occurs is the minor GC, which uses the stop-copy algorithm to clean up the new generation of memory into the larger Eden, and two equal survivor, each time you clean up the Eden And one of the surviving objects in a survivor is copied to another survivor, if the surviving object exceeds survivor memory, a portion of the object needs to be copied to the old generation through the space allocation guarantee mechanism , and then the Eden is cleared out. And the survivor just now. The memory capacity ratios of the Eden and survivor areas can be adjusted by the-xx:survivorration parameter. The default is 8,eden:survivor:survivor = 8:1:1.

Old generation: Major GC occurs. The object stored is much more than young generation, and there are many large objects, the old generation memory cleanup, if the use of stop-copy algorithm, rather inefficient, generally using the tag-collation algorithm, mark out the still surviving objects (there are references), Moves all surviving objects to one end to ensure continuous memory.

In the event of a minor GC, the virtual opportunity checks whether the size of each promotion into the old age is greater than the remaining space size of the old age, and if it is greater, it triggers the full GC directly, otherwise the-xx:+handlepromotionfailure is set (allowing the warranty to fail) , if allowed, only MINORGC is tolerated, memory allocation fails, and if not allowed, full GC is still performed (this means that if-xx:+handle promotionfailure is set, triggering MINORGC triggers the full GC at the same time. Even if there is a lot of memory in the old age, it is best not to do so.

Old generation GC Flag-purge method: Mark all objects that need to be recycled (accessibility analysis), and then uniformly clean up all tagged objects after the tag is complete.

The algorithm has two problems:

(1) Lower frequency problem: the labeling and removal process is not efficient

(2) Space problem: After the mark is cleared, there is a large amount of discontinuous memory fragmentation, and too much space fragmentation can lead to the inability to find enough contiguous memory when the running process needs to allocate large objects and to trigger another garbage collection in advance.

Old generation GC markup Grooming algorithm : The tagging process is the same as the tag cleanup algorithm, but the next steps no longer clean the recyclable object directly, but instead allows all surviving objects to move to one end and then clean out the memory outside the boundary.

permanent Generation: There are two types of garbage collection in a permanent generation: discarded constants and classes that are no longer used.

Obsolete constants: For example, we add a string constant "a" to a constant pool with intern (), but now the system does not have any string object called "a", so this constant is obsolete.

Classes that are no longer being used: ① all instances of the class have been reclaimed and no instances of the class exist in the Java heap.

The ClassLoader of the ② load class has been recycled

③ no Java.lang.Class object of the class is referenced, that is, the class information cannot be accessed through reflection.

Meet the above three conditions just meet the basic conditions of class recovery, whether the collection of unused classes need to see the set of-XNOCLASSGC parameters to control, you can also use-verbose:class and-xx:+traceclassloading,-xx:+ Traceclassunloading view load and unload information for a class.

Scenarios that use bytecode frameworks such as reflection, dynamic proxies, cglib, and dynamic generation of custom classloader such as JSPs and OSGi require virtual machines to have class offload capabilities to ensure that the permanent generation does not overflow.

Algorithm Analysis:

Space Allocation Guarantee:

Before executing the minor GC, the VM first checks to see if there is enough space in the old age to hold the new generation of surviving objects, and since the Cenozoic uses the replication collection algorithm , in order to improve memory utilization, only one of the Survivor is used as a rotational backup. So when a large number of objects are still alive after the minor GC, it is necessary for the old age to make the allocation guarantee, so that the survivor cannot accommodate the object directly into the old age, but only if the old age needs to have enough space to accommodate these surviving objects. However, the size of the surviving object is not explicitly known until the GC is actually completed, so before minor GC, the VM first checks whether continuous space in the old age is greater than the total size of the new generation object or the average size of successive promotions , and if the condition is true, minor GC, otherwise full GC (let the old age make more space). However, the average size of the previous promotion is also a certain risk, if a minor GC survived the sudden increase in the object, far above the average, it may still lead to the failure of the guarantee (Handle Promotion Failure, the old age can not store these objects), At this point, you have to re-launch the full GC after the failure (to make more space in the old age).

GC Collection objects are established:

Reference count: If there is a reference to this object, Object counter +1, reference invalidation, counter-1, in most cases, the algorithm is efficient and simple, but does not solve the relationship between the circular reference between objects, so it is not adopted by the mainstream language.

Accessibility algorithm: Through a series of objects called GC Roots as the starting point, and then search down; The path traversed by the search is called the reference chain/reference Chain, when an object is not connected to the GC Roots any reference chain , that is, the object is unreachable, which means that the object is not available, such as: OBJECT5, 6, and 7 , although interconnected, are not accessible to GC roots and are therefore also considered recyclable:

In Java, the objects that can be used as GC Roots include:

    1. Method Area: The object referenced by the class static property;
    2. Method Area: A constant reference to an object;
    3. The object referenced in the virtual machine stack (local variable table).
    4. The object referenced in the local method stack jni (native method).

Accessibility algorithm if the object to the GC roots is not to say that the object will be immediately recycled, it is necessary to go through a two-time tagging process, the first: is found in the accessibility analysis is not linked to the GC roots chain of reference. The second is to determine if a finalize () is required, and this method is the only chance for the object to be able to redeem itself, but it is not recommended, because this method is expensive, uncertain, and does not guarantee the order of execution of different objects. If you do not need to execute the method, it is recycled directly, and if you need to execute the method, the object will be placed in a f-queue. Although it will be executed, it is not necessarily guaranteed to succeed, as there may be unexpected situations such as a dead loop during the execution of this method, so the virtual machine does not necessarily wait for the method to be completed before it is reclaimed. If the object does not successfully save itself at the time of the second tag, it is really recycled.

Common JVM Configuration parameters

-xx:cmsinitiatingpermoccupancyfraction:Start the CMS recycle when the zone occupancy rate reaches this percentage
-xx:cmsinitiatingoccupancyfraction:Set the CMS collector to trigger after the old age space is used much
-xx:+cmsclassunloadingenabled:Allow recycling of class metadata
-xx:cmsfullgcsbeforecompaction:Set the number of times a CMS garbage collection is performed, and a memory compression
-xx:newratio:The ratio of the new generation to the old age
-xx:parallelcmsthreads:Set the number of threads for the CMS
-xx:parallelgcthreads:Set the number of threads to use for garbage collection
-xx:survivorratio:To set the size of the Eden area and the scale of the Survivior area
-XX:+USEPARNEWGC:Using the parallel collector in the Cenozoic
-XX:+USEPARALLELGC:The new generation uses the parallel collection collector
-XX:+USEPARALLELOLDGC:Old age using the parallel collection collector
-XX:+USESERIALGC:Using the serial collector in the new generation and the old age
-XX:+USECONCMARKSWEEPGC:New generation uses parallel collectors, older generations using cms+ serial collectors
-xx:+usecmscompactatfullcollection:Sets whether the CMS collector will defragment a memory fragment once it has completed garbage collection
-xx:+usecmsinitiatingoccupancyonly:Indicates that the CMS is recycled only when the threshold is reached

(+ cannot save )
-XMS: Sets the minimum space size for the heap.
-XMX: sets the maximum space size for the heap.
-xx:newsize Set the Cenozoic minimum space size.
-xx:maxnewsize Set the maximum space size for the Cenozoic.
-xx:permsize Sets the minimum space size for a permanent generation.
-xx:maxpermsize Sets the maximum space size for a permanent generation.
-XSS: set the stack size for each thread

Java's JVM Learning--Simple understanding of GC algorithms

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.