Java Virtual machine--garbage collection mechanism

Source: Internet
Author: User

Compared to C + +, Java has the technical advantage of dynamically allocating memory and garbage collection mechanisms , so that we don't have to focus on memory management, so why do we need to know about GC and memory allocation? The reason is simple: when you need to troubleshoot various memory overflows, When a memory leak problem occurs, when garbage collection becomes a bottleneck for the system to achieve higher concurrency, we need to implement the necessary monitoring and tuning of these "automated" technologies.

1. Why garbage Collection

With the running of the program, there are more and more object instances in the system memory, and if the garbage collection is not carried out, it will affect the performance of the program, and when it takes up too much memory, it will also produce system exceptions such as Oom.

2. Which memory needs to be recycled

The structure of the JVM memory is divided into: program counter, virtual machine stack, local method stack, heap, method area. The first three are born with the thread, the stack frame in the stack with the method's entry and exit and the orderly execution of the stack and the operation of the stack, when the method call ends or the thread ends, the memory will naturally be recycled, so only the heap and the method area needs a GC. So how do you identify the object of a GC? Simply put: If an object no longer has any references, it can be recycled.

3. When to recycle

  Reference counting algorithm to determine whether the object is alive: Add a reference counter to the object, and whenever there is a place to refer to it, the counter value is incremented by 1, the reference is invalidated by 1, and any object with counter 0 at any time is not available. This algorithm is not used in mainstream Java virtual machines because it has a big drawback: it is difficult to solve the problem of cross-referencing between objects.

  The Accessibility analysis algorithm determines whether an object survives: The basic idea is to start with a series of objects called "GC Roots", starting from these nodes, searching through the path called the reference chain, when an object to the GC Roots without any reference chain connected, proves that this object is not available. : Although Object5, 6, 7 are associated, it is unreachable for GC roots, so it is an object that can be recycled.

             

  Note : In the Java language, you can include the following types of GC roots objects: Objects referenced in the virtual machine stack (local variable tables in the stack frame) , objects referenced by class static properties in the method area, A method-area constant refers to an object that is referenced by the native method in the local method stack .

  expansion : After JDK 1.2, Java extends the concept of references into: strong, soft, weak, virtual , and progressively decreasing in intensity.

    Strong (Strong refence): Refers to the common in the program code, similar to "Object o = new Object ()" Such a reference, as long as the strong reference, the garbage collector will never reclaim the tune of the referenced object.

    Soft (Soft refence): Used to describe some objects that are useful but not necessary. For objects associated with a soft reference, the memory overflow exception is thrown when the memory overflow exception is placed in a recoverable range before the system will be reclaimed.

    Weak (Weak refence): It is also used to describe some useful but not necessary objects, but weaker than soft references, the objects associated with weak references can only survive until the next time the junk phone takes place.

    virtual (Phantom refence): The weakest of a reference relationship, its existence, does not affect the lifetime of its associated objects, or the virtual reference to obtain an instance of an object, its only role is to be able to receive a system notification when the associated object is recycled.

  To survive or to die?

  Even if the accessibility analysis algorithm is not reachable, it is not confirmed that "death", to truly declare an object "death", at least two times to go through the marking process: When the discovery is not reachable, the first mark and a filter, the condition is whether this object is necessary to execute the Finalize () method, When the object does not overwrite the Finalize () method or the Finalize () method has been called by the virtual machine, the virtual machine will be treated as "no need to execute" and if the object is judged to be necessary to execute the Finalize () method, it will be placed in the F-queue queue. And at a later time by a virtual machine automatically established by the low-priority finalizer thread to execute it, here "execute" refers to trigger this method. The Finalize () method is the last chance to get rid of "death", and later the GC will mark the object in F-queue for a second time.

  Summary: What kind of class needs to be recycled ? Conditions : 1> All instance objects of the class have been recycled 2> the ClassLoader that loaded the class has been recycled 3> the Reflection class Java.lang.Class object that corresponds to this class is not referenced anywhere.

4. How to Recycle

  tag Cleanup algorithm : Divided into "Mark" and "clear" two stages, mark all objects that need to be recycled, and then clear. Defect: The efficiency of marking and clear two phases is not high; generating large amounts of space debris can lead to a GC being carried out in advance with insufficient space allocated for large objects.

  Copy Algorithm : The memory is divided into two blocks of equal size, with only one piece, when a piece of space is full, the living object will be copied to another piece, and then the previously used memory space for a clean. Pros: Run efficiently without worrying about the creation of space debris. Defect: Split the memory in a slightly higher price. But, in the actual design of the virtual machine is not so divided, because the vast majority of the Cenozoic is "to die", so do not need 1:1 division, but divided into Eden:from survivor:to Survivor (8:1:1), when the memory is recycled, the Eden and from The surviving objects in the survivor are copied to another survivor.

  marker Finishing Algorithm : When the survival rate of the object is very high, the replication algorithm is not suitable, such as the old age. The tag grooming algorithm is to move the tagged objects to a section and clean the memory outside of the bounds directly.

  Generational Collection Algorithm : The current commercial virtual machine uses this algorithm, according to the new generation and the old era of the respective characteristics of the selection of the corresponding recovery algorithm. Cenozoic-replication algorithm; old age-"mark-clear" or "mark-organize".

5. Specific implementation of memory recovery----garbage collector

            

Note: If there is a connection between the two collectors, it can be used together.

  Serial collector : A single-threaded collector, in addition to using only a CPU or a garbage collection thread to recycle garbage, more importantly, it will stop the process when garbage collection, know the end of the collection, that is "Stop the world." Of course there are advantages: simple and efficient (single-line turndown with other collectors).

  parnew collector : The multi-threaded version of the serial collector is the preferred new generation collector in virtual machines running in server mode, one of the most important reasons is that it can be used in conjunction with a CMS.

  Parallel Scavenge collector : A parallel, use copy algorithm, acting on the new generation of collectors, it is different from the CMS and other collectors, the focus is not the same. The CMS focuses on minimizing the downtime of user threads when garbage collection is as short as possible, while the parallel scavenge collector achieves a controllable throughput (running user code time/(running user code time + garbage Collection Time)), which is primarily suitable for tasks that operate in the background without requiring too much interaction.

  Serial Old collector : The older version of the Serial collector that uses the "mark-and-organize" algorithm, the meaning of this collector is to be used for virtual machines in client mode.

  Parallel Old collector : The older version of Parallel scavenge, using multithreading and the "mark-and-organize" algorithm.

  CMS collector : is a collector to obtain the shortest payback time, based on the "mark-clear" algorithm implementation, the operation process is divided into four steps: initial tag , concurrency tag , re-tagging , Concurrent Cleanup .

Initial tag: Marks the object that the GC roots can directly relate to, fast.

Concurrency token: The process of making GC Roots tracing (Judging whether an object is recyclable).

Re-tagging: Fixed a tag record for the part of the object that caused the tag to change during the concurrent tag because the user program continued to work.

Concurrent purge: Clears recyclable objects. together with the concurrent tagging process, you can work with the user thread .

  Benefits of CMS : Concurrent collection, low pause. The disadvantages are also obvious, as follows :

The CMS is very sensitive to CPU resources. In the concurrency phase, the user thread does not pause, but consumes CPU resources, causing the program to slow down and the total throughput to decrease.

CMS could not handle floating garbage, may appear "Concurrent Mode Failure" failure resulting in another full GC. Because the concurrent cleanup phase and the user thread are running synchronously, new garbage is constantly generated, and after being flagged, it cannot be reclaimed at the time of the garbage collection, but until the next time, these become "floating garbage". JDK 1.5 By default the old age uses 68% space to activate the CMS, which makes room for the reason of concurrency.

The CMS generates a lot of space debris based on "mark-erase".

 Note : concurrent (Concurrent): The user thread executes concurrently with the garbage collection thread (not necessarily parallel, possibly alternately), the user program continues to run, and the garbage collector runs on the other CPU.

    Parallel (Parallel): Refers to multiple garbage collection threads working in parallel, but at this point the user thread is still in a wait state.

  G1 collector : A service-oriented garbage collector with the following features:

▲ Parallel and Concurrency: Set the advantages of two ways in one.

▲ Generational collection: As with other collectors, although the G1 does not require other collector mates to manage the entire GC heap, it can be used in different ways to manage the new generation and the old age, and it works well.

Spatial integration: Overall, based on "tag-collation", the partial view is based on "copy", no matter how the view will not produce space debris.

  

    

  

  

  

Java Virtual machine--garbage collection mechanism

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.