JVM Summary (ii): garbage collector

Source: Internet
Author: User

In this section, let's summarize what the JVM garbage collector does.

Garbage collector
Determine if an object reference is invalid
Object Survival Judgment algorithm
Reference judgment Process
Introduction to garbage collection algorithms
Garbage collector
New Generation garbage collector
Old age garbage collector
New generation and old age garbage collector garbage collectors determine whether object reference is invalid object survival judgment algorithm
    • Reference counting Method
      Add a reference counter to the object, each time a place refers to this object, the counter value is added 1, when the reference fails, the value of the counter is reduced by 1, when the counter value becomes 0 o'clock, it is impossible to use the object.
      Advantages: Simple to achieve, high efficiency of judgement.
      Cons: When there are mutual circular references between objects, that is, two classes have reference fields referencing each other, the algorithm is not valid during the recycling process.
    • Accessibility Analysis Algorithm
      In order to overcome the disadvantage of reference counting method, the main realization algorithm is now the Accessibility analysis algorithm. The basic idea of the algorithm is to start with a series of objects that become "GC Roots", starting from these nodes, and searching through the path called the reference chain. This object is not available if, in a single search, an object is connected to the GC roots without any reference chain. Specific

GC Roots's decision:
1. Objects referenced in the virtual machine stack (local variable table in the stack frame)
2. Objects referenced by static properties in the method area
3. Objects referenced by constants in the method area
4. The object referenced by JNI (that is, generally referred to as the native method) in the local method stack

Reference judgment Process

the process of judging if a reference is invalid is divided into three stages
1, when the JVM is garbage collected, the JVM uses the Accessibility analysis algorithm for analysis, if the object after the accessibility analysis found that there is no reference chain connected to GC roots, the object will be first marked and filtered, the condition is that this object does not need to perform finalize ( ) method, if the object does not overwrite the method, or if the method has been called by the virtual machine, the virtual machine treats both cases as "no need to execute."
2, if the object is determined to be necessary to execute the Finalize () method, then the object will be placed in a queue called F-queue, and later by a virtual machine automatically established by a low-priority finalizer thread to execute it. Here the so-called execution means that the virtual opportunity triggers this method, but does not commit to waiting for it to end. Because an object may be slow to perform in the Finalize () method, or a dead loop occurs, this causes other objects in the queue to wait for long periods of time, even causing the entire memory system to collapse.
3, F-queue in the tag filter.
The Finalize () method is the last chance for an object to escape the fate of death, and then the GC will mark the second small-scale object in the F-queue. If the object succeeds in saving itself in the Finalize () method, which is associated with any object on the reference chain, the algorithm is moved out of the F-queue collection at the second token, and if the object does not escape at this time, it is basically recycled.

Introduction to garbage collection algorithms

At present, there are four kinds of garbage collection algorithms: Mark-Clear algorithm, copy algorithm, marker-collation algorithm and generational collection algorithm. The detailed analysis compares the following:

category tag-purge algorithm (mark-sweep) replication Algorithm (coping) flag-Sorting algorithm (mark-compact) Generational collection algorithm (generational Collection)
to organize Whether Is Is Is
Algorithm implementation process The algorithm is divided into two processes: tag and purge. All objects that need to be reclaimed are marked first, and all objects that are marked for recycling are collected uniformly after the tag is completed. Divide memory by capacity into two areas of equal size, each time using one piece, when a piece of memory is exhausted, the GC algorithm is executed to copy the surviving objects to another piece, and then clean up all the memory blocks. The algorithm is divided into two processes: tagging and collating. First mark out all the objects that need to be recycled, and then let the surviving objects move toward one end of the memory, and then directly clear out the memory outside the end boundary. The memory is divided into several blocks according to the life cycle of the object, which is divided into the new generation and the old age, then the most appropriate collection algorithms are adopted according to the characteristics of each age.
Advantages Simple and easy to implement Memory allocation algorithm does not generate memory fragmentation Memory allocation algorithm does not produce memory fragmentation, it is also easier to implement High efficiency by generational collection
Disadvantages 1, low efficiency 2, will produce a large number of discontinuous memory fragments Space consumption is too high, memory is compressed to half the original High complexity of the algorithm, more steps to execute High complexity of the algorithm, more steps to execute
Garbage collector

There are seven common JVM garbage collectors, as shown in the following:

New Generation garbage collector
  • Serial
    The serial collector is a single-threaded collector, but its single-threaded meaning is that it uses only one CPU or one collection thread to do garbage collection, and more importantly, when it is garbage collected, all other worker threads must be paused until it is collected.
    Generational collection algorithm: The Cenozoic single thread uses the replication algorithm and pauses all user threads; the old-time single thread takes the tag-collation algorithm and pauses all user threads.
  • Parnew
    The Parnew Collector is a multithreaded version of the serial collector. Its basic operation and serial algorithm are basically consistent. The collector typically works with the CMS collector. '
    Generational collection algorithm: The new generation adopts the replication algorithm and pauses all user threads; The old age uses the tag-collation algorithm and pauses all user threads.
  • Parallel Scavenge
    The Parallel scavenge collector is also very similar to the parnew algorithm, but most of the concerns with other collectors are to minimize the downtime of the user thread when garbage collection occurs, while the Parallel scavenge collector is designed to achieve a controllable throughput. Throughput is the ratio of the CPU's time spent running user code to the total CPU time spent. That is, throughput = Run user code time/(run user code time + garbage collection time), for example, the virtual machine runs for a total of 100 minutes, where garbage collection takes 1 minutes, and that throughput is 99%.
    GC Adaptive throttling Policy: the JVM collects performance monitoring information based on the current system's operation, dynamically adjusting these parameters to provide the most appropriate pause time or maximum throughput. The Parallel scavenge collector can be paired with an adaptive adjustment strategy.
    Generational collection algorithm: The new generation adopts the replication algorithm and pauses all user threads; The old age uses the tag-collation algorithm and pauses all user threads.
Old age garbage collector
  • Serial Old
    Serial old is an older version of the Serial algorithm, and is also a single-threaded collector. This collector is primarily for use with virtual machines in client mode.
    Generational collection algorithm: The Cenozoic single thread uses the replication algorithm and pauses all user threads; the old-time single thread takes the tag-collation algorithm and pauses all user threads.
  • Parallnel Old
    Parallnel old is an older version of the parallel scavenge collector, using multithreading and the "mark-and-organize" algorithm.
  • CMS
    The CMS (Concurrent Mark Sweep) collector is a collector that targets the shortest recovery pause time. The collector is implemented based on the "tag-purge" algorithm.
    The collection process for the CMS collector is divided into the following 4 steps:
    1. Initial tag (Stop the world, mark the object that GC roots can directly relate to)
    2, concurrent tagging (the process of GC Roots tracing)
    3. Re-mark (Stop the world, break the tag record of the part of the object that caused the tag to change during the time the user program continues to run)
    4. Concurrent Cleanup (concurrent cleanup of useless objects)
    Disadvantages:
    A, the CMS collector is very sensitive to CPU resources, and the concurrent phase consumes more thread resources.
    B, the CMS collector cannot handle floating garbage. Because the CMS concurrent cleanup phase user thread is still running, so there will be a corresponding garbage generation, this part of the garbage CMS can not dispose of them in this collection.
    C, the CMS collector is based on the "tag-purge" algorithm, resulting in more memory space fragmentation.
New generation and old age garbage collectors
  • G1
    Features of the G1 (Garbage-first) Collector:
    1, parallel and concurrency: Using multiple CPUs to shorten the stop-the-world time, some garbage collectors originally need to pause the Java thread to perform the GC action, the G1 collector can still be in a concurrent way for Java programs to continue to execute.
    2. Collection of Generations
    3. Spatial integration: marker-collation algorithm.
    4. Predictable pauses. The pursuit of low pauses and the creation of a predictable pause-time model allows the user to explicitly designate a time fragment with a length of m milliseconds to consume on garbage collection for no more than n milliseconds, reaching a real-time Java garbage collector.
    G1 Collector generational Strategy:
    The G1 collector divides the entire Java heap into separate regions of equal size (region). The G1 collector can systematically avoid full-area garbage receipts throughout the Java heap because the G1 collector tracks the value of garbage accumulation in each region (the amount of space reclaimed and the amount of time it takes to reclaim), maintaining a prioritized list in the background, Priority is given to recovering the region with the greatest value, per the allowable collection time. That is Grabage-first.
    In the G1 collector, the object reference between region and the new generation to the old age in other collectors, the virtual machine is
    Remembered set to avoid a full heap scan. Each region in the G1 has a corresponding remembered Set. When you create a new object, the JVM logs the relevant reference information to the remembered set in the region to which the referenced object belongs. When recycling, adding the remembered set to the enumeration scope of the GC root node guarantees that the heap is not scanned or omitted.
    The mobile stage of the G1 collector is also divided into the following steps:
    1, the initial tag (just mark the GC roots can directly relate to the object, and modify can be created in the region to create a new object, this stage needs to pause the thread, but time is very short)
    2, concurrency tag (starting from GC roots for the analysis of the objects in the heap to find the surviving objects)
    3, the final mark (fixed during the concurrent tag due to the month Honghu program continues to run, which marks the change of the part of the mark Record)
    4, screening and recycling (first of all Regin recovery value and cost of sorting, according to the user expected GC pause time to specify the recovery plan, recover part of region)

Finally, let's summarize the garbage collector in the JVM:

Generational
category ownedUsing Threads using Algorithms
Serial Cenozoic Single Thread Copy (new), Mark-organize (old)
Parnew Cenozoic Multithreading Copy (new), Mark-organize (old)
Parallel Scavenge Cenozoic Multithreading Throughput first algorithm
Serial old Laosheng generation Single Thread Copy (new), Mark-organize (old)
Parallel old Laosheng generation Multithreading Copy (new), Mark-organize (old)
Cms Laosheng generation Multithreading Tag-purge algorithm (initial tag, concurrency token, re-tagging, concurrency cleanup)
G1 New Generation && Laosheng generation Multithreading Tag-Grooming algorithm (initial tag, concurrency tag, final tag, filter recycle)

JVM Summary (ii): garbage collector

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.