Jvm determines whether the object is garbage

JVM algorithm for judging whether it is garbage
1. Program counter (repeated references and circular references may occur)
to derive reachability algorithm gc roots
2. Reachability analysis is a way to determine garbage in JVM garbage collection. Another method is the reference notation, but the reference notation cannot solve the circular reference problem.

public class Main {
    
    
    public static void main(String[] args) {
    
    
        MyObject object1 = new MyObject();
        MyObject object2 = new MyObject();
 
        object1.object = object2;
        object2.object = object1;
 
        object1 = null;
        object2 = null;
 
        //最后面两句将object1和object2赋值为null,也就是说object1和object2指向的对象已经不可能再被访问,但是由于它们互相引用对方,导致它们的引用计数都不为0,那么垃圾收集器就永远不会回收它们。
    }
}
 
class MyObject{
    
    
    public Object object = null;
}

In the Java language, the objects that can be used as GC Roots include the following:
Insert picture description here

Objects referenced in the virtual machine stack (local variable table in the stack frame). (It can be understood as: refer to all the objects of the local variable table in the stack frame)
the object referenced by the static property in the method area (can be understood as: refer to all the objects of the static property in the
method area ) the object referenced by the constant in the method area (understandable For: all objects that refer to the constants in the method area
) the objects referenced by the (Native method) in the local method stack (can be understood as: all objects that refer to the Native method)

2.finalze

The object saves itself. When the object changes from reachability to unreachable, it first becomes restored. At this time, it may go back and call the finalize method
. 1. The subclass must renew the method
. 2. The method can only be executed once
and meets the above requirements. Two situations --->The execution of the underlying finalizer thread The
finalize() method finally determines whether the object is alive:

即使在可达性分析算法中不可达的对象,也并非是“非死不可”的,这时候它们暂时处于“缓刑”阶段,要真正宣告一个对象死亡,至少要经历再次标记过程。
标记的前提是对象在进行可达性分析后发现没有与GC Roots相连接的引用链。

1). Mark for the first time and perform a screening.

  筛选的条件是此对象是否有必要执行finalize()方法。
  当对象没有覆盖finalize方法,或者finzlize方法已经被虚拟机调用过,虚拟机将这两种情况 都视为“没有必要执行”,对象被回收。

2). The second mark

如果这个对象被判定为有必要执行finalize()方法,那么这个对象将会被放置在一个名为:F-Queue的队列之中,并在稍后由一条虚拟机自动建立的、低优先级的Finalizer线程去执行。这里所谓的“执行”是指虚拟机会触发这个方法,但并不承诺会等待它运行结束。这样做的原因是,如果一个对象finalize()方法中执行缓慢,或者发生死循环(更极端的情况),将很可能会导致F-Queue队列中的其他对象永久处于等待状态,甚至导致整个内存回收系统崩溃。
Finalize()方法是对象脱逃死亡命运的最后一次机会,稍后GC将对F-Queue中的对象进行第二次小规模标记,如果对象要在finalize()中成功拯救自己----只要重新与引用链上的任何的一个对象建立关联即可,譬如把自己赋值给某个类变量或对象的成员变量,那在第二次标记时它将移除出“即将回收”的集合。如果对象这时候还没逃脱,那基本上它就真的被回收了。

heap

After 1.7, 1.7 canceled permanent generation and changed to meta space

  • The new generation of the heap
    Eden: to survivor: from survivor ==8:1:1
    Eden-> to survivor -> from survivor is continuously garbage collected in the process, and then copied to the next area, from survivor to to survivor is called one year old , The process continues to loop until it reaches the age of 15, and objects that have not been recycled will enter the old area. When the Eden area is used up, garbage collection will be triggered, and the surviving ones will move to the 1 (to survivor) area.) The
    new generation garbage collection algorithm Is minorGc
  • The ratio of the
    old generation to the new generation is 2:1 . The garbage algorithm of the old generation is mark sorting and mark removal (fullGc)
  • Major GC is to clean up the permanent generation. The permanent generation is cancelled in 1.8
    . The nature of the meta space is similar to that of the permanent generation. They are both the realization of the method area in the JVM specification. However, the biggest difference between metaspace and permanent generation is that metaspace is not in the virtual machine, but uses local memory. Therefore, by default, the size of the meta space is only limited by the local memory (the specifics are not clear)
  • Method area and heap are shared by threads

jvm memory

The default size of jvm memory is 1/64 of local memory, and the maximum attempted memory is 1/4 of local memory

https://blog.csdn.net/qq_38905818/article/details/102500173

Guess you like

Origin blog.csdn.net/qq_38893133/article/details/103882132