The concept of GC

Garbage Collection Garbage Collection. The objects of GC in Java are heap space and persistent areas.Copy the code

GC algorithm

Reference counting method
  • Concept: Reference counting is an old garbage collection algorithm that uses references to objects to calculate whether or not to recycle. Com, ActionScript3, Python have all used it.
  • Principle: For an object A, the reference counter of A increases by 1 whenever any object is referenced, and decreases by 1 if the reference is unreferenced. If the counter is zero, the object is garbage
  • Disadvantages: Reference and removal references are accompanied by addition and subtraction, which affects performance. Circular references are also difficult to handle (see figure below).
  • Pros: Instant collection, because the object knows when it’s useless
Mark clear
  • Concept: The tag-sweep algorithm is the ideological basis of modern garbage collection algorithms.
  • How it works: This algorithm divides garbage collection into two phases, marking phase and cleaning phase. One possible implementation is to mark all reachable objects from the root node first through the marking phase. So unmarked objects are unreferenced junk objects. Then in the purge phase, unmarked objects are cleared. (See below)
  • Cons: Gradually producing refined chunks, which soon result in countless smaller chunks scattered throughout the heap. The memory will be divided up, so that when allocating objects, you have to traverse the available memory blocks first.
  • Advantages: Simple to implement
Mark compression
  • Concept: The mark-compression algorithm is suitable for situations with many living objects, such as the old age. It makes some optimizations based on the mark-clear algorithm
  • How it works: As with mark clearing, mark but then clear compresses all surviving objects to one end of memory, and then cleans up all space outside that boundary
  • Disadvantages: In the cleanup algorithm, the cleanup phase also searches the entire heap, but only once is enough. But the GC tag-compression algorithm takes about three times as long to search three times, which is a huge disadvantage, especially as the heap size increases the cost (see figure)
  • Advantages: high heap utilization efficiency. And the GC mark-compression algorithm does not utilize only half the heap as the GC replication algorithm does
Replication algorithm
  • Concept: Improved on the basis of mark clearing

  • Principle: The original memory space is divided into two parts and only one part is used each time. During garbage collection, the surviving objects in the used memory are copied to the unused memory block. After that, all objects in the used memory block are cleared and the roles of the two memory blocks are changed to complete garbage collection

  • Cons: Look at pictures

  • Advantages:

    1. Excellent throughput: The throughput consumed by the GC tag-sweep algorithm is the sum of the time spent searching the live objects (the tagging phase) and the time spent searching the whole heap (the sweep phase). On the other hand, because the GC replication algorithm only searches and copies live objects, it can complete GC in a shorter time than the normal GC mark-sweep algorithm. In other words, its throughput is excellent. 2. High-speed allocation: GC replication algorithms do not use idle linked lists. This is because a partition is a contiguous memory space. The GC replication algorithm is significantly faster than allocation using free linked lists, such as GC mark-sweep and reference counting. 3. Fragmentation will not occurCopy the code
Summary of GC Algorithm

In conclusion, I think it’s better to introduce the idea of generations

According to the life cycle of objects, short-lived objects are classified as Cenozoic, long-lived objects are classified as old.

According to the characteristics of different generations, appropriate collection algorithms are selected

  • A small number of objects survive, suitable for replication algorithms
  • A large number of objects survive and are suitable for tag cleaning or tag compression

Can hit a sexual

Q: In the GC algorithm, how to determine whether an object is garbage is based on accessibility, so what is accessibility?

A:

  • Objects referenced in the stack
  • Objects referenced by static members or constants in a method area (global objects)
  • Reference objects in the JNI method stack

The other thing that doesn’t make this object junk is that it can be resurrected

Once all references are released, it is a revivable state, because it is possible to revive the object in Finalize ()Copy the code

But this is generally not used:

Avoid finalize(), careless operation may lead to errors. Lesson: Avoid using Finalize (), careless operation may lead to errors. Low priority, not sure when it will be called, not sure when GC will happen Not sure you can use try-catch-finally insteadCopy the code

You can try it out in code here

public class CanReliveObj {
   public static CanReliveObj obj;
   @Override
   protected void finalize() throws Throwable {
       super.finalize();
       System.out.println("CanReliveObj finalize called");
       obj=this;
   }
   @Override
   public String toString() {return "I am CanReliveObj"; } public static void main(String[] args) throws InterruptedException{ obj=new CanReliveObj(); obj=null; 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; System.gc(); Thread.sleep(1000);if(obj==null){
System.out.println("Obj is null");
}else{
System.out.println(Obj "available"); }}Copy the code

Stop-The-World

For more information, visit juejin.cn/post/684490…

A global pause in Java in which all Java code stops. Native code can execute but cannot interact with the JVM, mostly due to GC causing Dump threads, deadlock checks, and heap dumps.

Why is there a global pause during GC? The analogy is to clean the room at a party. When the party is very messy, and there is new garbage, the room can never be cleaned up. Only when everyone stops activities can the room be cleaned up.

Harm: Service stops for a long time, and the HA system does not respond. Active/standby switchover may occur, which seriously damages the production environment.