gc garbage collection diagram

Directory:
java virtual machine summary

  1. Class file structure analysis
    1). The constant item structure in the class file constant pool
    2). A collection of commonly used attribute tables
  2. Class loading process
    1). Principle and implementation of class loader
  3. Virtual machine structure analysis
    1) .jdk 1.7 and 1.8 version of the method area structure changes
    2). Simple distinction between constant pools
  4. Object structure analysis
    1). Detailed explanation of compressed pointers
  5. gc garbage collection <<== present position
  6. Object positioning method

What is rubbish?

When there is no reference to the object we created, the object can no longer be used, that is, the object has become a garbage object and has been stored in our memory.
Example:
Student s =new Student();
s = null;

How do we judge whether an object is garbage

Two ways

  1. Reference counting method
    This method is based on whether the object has a reference point, for
    example:
    Student s =new Student(); //The number of references to this Student object is 1
    Student s2 = s; //The number of references to this object is 2 at this time
    When the reference of an object is 0, it is judged as garbage.
    Problem:
    If there is an attribute in the object A that points to B in the following code, and there is also an attribute in B that points to A, then the number of references to the two objects will not be 0, but the fact is that neither of these two objects can be used anymore, it’s rubbish
class TestB{
    
    

   public TestA testa;

    public TestB(TestA testa) {
    
    
        this.testa = testa;
    }
    
    public static void main(String[] args) {
    
    
        TestA testa = new TestA();
        TestB testb = new TestB(testa);
        testa.testb = testb;
        testa = null;
        testb = null;
    }
}
public class TestA {
    
    
    public TestB testb;

    public void setB(TestB b) {
    
    
        this.testb = testb;
    }
}

  1. Reachability analysis method
    The second method of judging whether an object is garbage, GCRoot root search method.
    This method starts from a GCRoot root, all dependent objects are marked as not garbage, and the others are garbage
    GCRoot roots:
    1 Objects referenced by class static properties in the heap
    2. Objects referenced by variables in the
    method area 3. Objects referenced by constants in the method area
    4. Objects referenced by the local variable table in the virtual machine stack
    5. Objects referenced by the local method stack (that is, native The object referenced in the method)
    looks very confusing? It doesn't matter,
    for example (see note)
class TestB{
    
    

   static TestA a1= new TestA(); //对应上面的第一个
   final TestA a2 = new TestA();  //对应上面第二个
   TestA a3 = new TestA();//对应上面第三个
  
   public void method1(){
    
    
       TestA a = new TestA(); //对应上面的第四个
   }
}

How to recycle garbage

Knowing what is rubbish, we consider how to remove this rubbish

  1. Copy algorithm The
    space is divided into two, one side put the object, the other side does not put anything, empty and
    Insert picture description here
    start to clean up the garbage, copy the other part of the surviving object, directly empty the original space, and then use the space on the right as the object storage place, on the left It becomes an area where nothing is put, and the
    Insert picture description here
    efficiency of clearing is high. It is suitable for an area with a lot of garbage and few survivors. In this way, we only need to move a few surviving ones. But if there is very little garbage and a lot of survivors, Then we need to copy a lot of surviving objects to another storage space, which is inefficient, and this method is very space-consuming, after all, only half is used

  2. Mark removal
    is very simple to mark all the rubbish and clear all the rubbish directly.
    Insert picture description here
    Insert picture description here
    This method will generate a lot of memory fragments, and other large objects cannot be stored when they come, and the efficiency is moderate.

  3. Marking and arranging
    On the basis of the above, clean up after clearing, so that the objects are arranged in a regular
    Insert picture description here
    way. There is no fragmentation in this way. However, you have to mark and clear and move to the designated area, which is equivalent to copying again, so the efficiency is not high.

Garbage collection in the hostSpot virtual machine heap

After understanding how to judge whether it is garbage, and how to recycle garbage, see which methods are used in our virtual machine

Our virtual machine uses encapsulation technology to encapsulate several garbage collectors to collect garbage. Next, analyze the explanation of each garbage collector.
1. The generational garbage collector will
Insert picture description here
Insert picture description here
first say that we use jdk1.8 by default. ps+po handles the Parallel Scavenge and Parallel Old in the picture

Insert picture description here
In this way, we first understand the Parallel pair.
Parallel starts with multi-threaded execution. The
young generation is
in the young generation. Because most of the objects are dying day and night, many of them are garbage, and only a few survive, so Parallel Scavenge uses replication. Algorithm, the above figure is divided into Eden area and two survive areas, why are they not half of the memory? But 8:1:1
he works like this:

  1. Place objects in the Eden area. At this time, there are no objects in the two survivor areas. When garbage collection occurs, all the surviving objects in the Eden area are copied to the survival1 area. At the same time, the Eden area and the Survive2 area are cleared, and then the objects are placed in the Eden area, and garbage is again Recover, copy the surviving objects in the Eden area and the survive1 area to the survive2, empty the survive1 and the Eden area, and go back and forth. . . Parallel Scavenge is copied like this, and then every garbage collection will add one to the generation age (4 digits) on the head of the object. When it reaches 15, the object is copied to the old generation.

The
work of Parallel Old in the old age
uses the mark-arrangement algorithm, which is the same as the mark-arrangement algorithm we talked about here, so I won’t go into details.

If you need a detailed understanding of each garbage collector, directly Baidu

Okay, we have finished talking
about the commonly used garbage collector. Let me talk about some common sense
. The gc that occurs in the young generation is youngGC. The one that occurs in the old generation is FullGC. FullGC only occurs when there is insufficient memory. At this time, many objects in the old generation will be collected at one time. , Release memory, youngGC is very frequent, and here involves stw (stopTheWord), which means that all other threads cannot work during garbage collection, that is, the server will not be able to process any requests at this time, so try to reduce the time of fullGC and stw as GC The key to tuning,

Here we go back to the garbage collector to
understand ps+po. We are here to understand the others,
Insert picture description here
which is also well understood. Serial refers to a single line, that is, only one thread is performing the garbage collection task. The effect is definitely not as good as the Parallel multi-threaded garbage collector. Please
Insert picture description here
explain the difference between the two garbage collectors of Baidu . ParNew is a new multi-threaded collector, specifically Baidu, and CMS is a cross-age garbage collector, because it marks garbage concurrently with other threads. Executed, that is to say, when other garbage collectors are working, the server cannot respond, and CMS is concurrent, which means that when our gc is working, other threads can also be executed, which is an epoch-making structure, (understand That is to say, there may be some problems in CMS, which will not be discussed in detail here, Baidu)
2. Non-generational garbage collector
Insert picture description here
This is another cross-generation garbage collector, because it abandons the traditional generational generation, young generation old generation In the ages, garbage collection is logically carried out regardless of generations (you can understand, every garbage collector will take a long time to talk about it, here is the context, specifically Baidu)

Some other little knowledge (interview):
what is a strong quotation, weak quotation, soft quotation, false quotation,

Guess you like

Origin blog.csdn.net/lioncatch/article/details/106070460