In-depth JVM "Four" GC Algorithms and types

Source: Internet
Author: User
Tags garbage collection throwable


First, the concept of GC garbage Collection garbage collection.
1960 The List uses a GC.
In Java, GC objects are heap space and a permanent zone.


Second, GC algorithm
1. Reference counting method(not used by Java, in Python)Veteran garbage collection algorithm.
Garbage is recycled by reference calculations.

The implementation of the reference counter is simple, for an object A, as long as any one object refers to a, then the reference counter of A is incremented by 1, and when the reference is invalidated, the reference counter is reduced by 1. Object A can no longer be used as long as the value of the reference counter for object A is 0.

Problem:
1. Reference and dereference accompanying addition and subtraction, affecting performance
2, it is difficult to deal with circular references, if at the root node and not reference, but the child nodes are referenced to each other, then still cannot be recycled.


2, mark the idea of removing the modern garbage collection algorithm.
The tag-purge algorithm divides garbage collection into two phases: the tagging phase and the purge phase. A feasible implementation is to mark all the objects that can be reached from the root node, first through the root node, in the tagging phase. Therefore, an object that is not marked is a garbage object that is not referenced. Then, in the purge phase, all unmarked objects are cleared.


3. Tag compressionthe tag-compression algorithm is suitable for applications where there are many surviving objects, such as the old age.
It does some optimizations based on the mark-and-sweep algorithm. Like the mark-and-sweep algorithm, the tag-compression algorithm first needs to start with the root node and mark all objects that can be reached. However, it does not simply clean up unmarked objects, but instead compresses all the surviving objects to one end of the memory.after that, clean up all the space outside the boundary.

4, the replication algorithm compared with the mark-clear algorithm, the replication algorithm is arelatively efficientMethod of recycling.
not suitable for situations where there are many surviving objects such as the old age.
Divide the original memory space into two blocks, one at a time, and at garbage collection, copy the surviving objects in the memory being used into the unused block of memory, then clear all objects in the memory block in use, swap the two memory roles, and complete the garbage collection.

The biggest problem with the replication algorithm is:Wasted Space    We integrate the idea of tag cleanup to implement (generational GC algorithm):In the S0/s1 area, each copy of the collection will give you age plus one. When it reaches a certain age, it is considered an old age object, which is copied directly from the S-area to the old age.

According to the survival period of the object, the short-lived object is classified as the new generation and the long-life object belongs to the old age. According to the characteristics of different generations, select the appropriate collection algorithm: a small number of objects Survive "Cenozoic", suitable for the replication algorithm. (The arrows in all are copy algorithms) A large number of objects survive the "old age", suitable for tag cleanup or tag compression. (used in the old age)


Third, the accessibility of all algorithms, need to be able to identify a garbage object, so need to give a definition of the accessibility.

Accessibility:
1. Accessible: This object can be touched from the root node
2. Can be resurrected:
Once all references have been released, it is a resurrected state
Because the object may be resurrected in Finalize ()
3. Untouchable(objects that can really be recycled):
After Finalize (), it may enter a non-palpable state
Untouchable objects cannot be resurrected.
Can be recycled

Finalize () will only be called once.


  Package Com.jvm.stack;public class Canreliveobj {public static canreliveobj obj;        @Override protected void Finalize () throws Throwable {super.finalize ();        System.out.println ("Canreliveobj finalize called");    obj = this;//here makes the Obj object point to itself, that is, the object is composited.    } @Override Public String toString () {return "I am Canreliveobj"; public static void Main (string[] args) throws interruptedexception {Canreliveobj obj = new Canreli        Veobj ();   obj = null;        Resurrection System.GC ();        Thread.Sleep (1000);        if (obj = = null) {System.out.println ("obj is null");        } else {System.out.println ("obj available");        } System.out.println ("second GC");    obj = null;        Non-resurrected System.GC ();        Thread.Sleep (1000);        if (obj = = null) {System.out.println ("obj is null");        } else {System.out.println ("obj available"); }    }}
<textarea spellcheck="false" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none" tabindex="0" readonly=""></textarea>
X
1
 Package com. JVM. stack;
2
3
 Public class Canreliveobj {
4
 Public Static Canreliveobj obj;
5
6
@Override
7
protected void Finalize throws Throwable {
8
Super. Finalize ();
9
System. out. println ("Canreliveobj finalize called");
10
obj = this; //Here the Obj object points to itself, i.e. the object is composited. 
11
  }
12
13
@Override
14
 Public String toString () {
15
return "I am Canreliveobj";
16
  }
17
18
 Public Static void Main (Stringargsthrows
19
interruptedexception {
20
Canreliveobj obj = New Canreliveobj ();
21st
obj = NULL //Can be resurrected
22
System. GC ();
23
Thread. Sleep (+);
24
if (obj= =null) {
25
System. out. println ("obj is null");
26
  Else {
27
System. out. println ("obj available");
28
  }
29
System. out. println ("Second GC");
30
obj =    null; //non-resurrected
31
System. GC ();
32
Thread. Sleep (+);
33
if (obj= =null) {
34
System. out. println ("obj is null");
35
  Else {
36
System. out. println ("obj available");
37
  }
38
  }
39
}
40

Experience: Avoid using Finalize () and careless operation can cause errors.(if the second obj=null is not set, it will cause obj to not be recycled)Low priority, when called, not sure because when it happensGC not sureYou can use try-catch-finally to replace it.


Definition of root:
Objects referenced in the stack in the method area of a static member or a constant reference object (global object) The Reference object in the Jni method stack


Iv. Stop-the-world a phenomenon of global pauses in Java. Global pauses, all Java code stops, native code can execute, but cannot interact with the JVM.
Mostly caused by GC.
Dump thread deadlock Check heap dump


Why is there a global pause when GC?        Analogy in the party to clean the room, the party is messy, and new garbage produced, the room is never clean. The room can be cleaned only if you stop the activity.

the new generation of GC is very short.
the GC of the old age is sometimes shorter and sometimes very long. (It may take several minutes to set a few 10 minutes)

The service stopped for a long time and did not respond. Encountering an HA system can cause primary and standby switching, seriously compromising the production environment.







In-depth JVM "Four" GC Algorithms and types

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.