[Java] JVM and garbage collection

JVM

Article Directory


JVM structure:

  • ClassLoader ClassLoader : used to load .class files
  • Execution engine : execute bytecode, or execute native method
  • Runtime data area : method area, heap, Java stack, program counter, local method stack

img

**JVM principle: **Java source files are compiled into bytecode programs, and each instruction is translated into machine code of different platforms through JVM, and run on a specific platform.

JVM execution program process:

1. Load the .class file

2. Manage and allocate memory

3. Perform garbage collection

Four steps to complete the JVM environment:

1. Create JVM loading environment and configuration

2. Load JVM.dll

3. Initialize JVM.dll and connect to JNIENV (JNI call interface) instance

4. Call the JNIEnv instance to load and process the class.

Class loader

Life cycle:

img

Reference to: https://blog.csdn.net/know9163/article/details/80574488

Garbage collection

Method to determine whether the object is alive:

  • Reference counting method:

    Reference counting is an early strategy in the garbage collector. In this method, each object instance in the heap has a reference count. When an object is created, the object instance is assigned to a variable, and the variable count is set to 1. When any other variable is assigned as a reference to this object, the count is increased by 1 (a = b, then the counter of the object instance referenced by b + 1), but when a reference of an object instance exceeds its life cycle or is set to At a new value, the reference counter of the object instance is decremented by 1. Any object instance with a reference counter of 0 can be considered garbage collected. When an object instance is garbage collected, the reference counter of any object instance it references decrements by one.

    Advantages: The reference counting collector can be executed very quickly, intertwined in the running of the program. It is more beneficial to the real-time environment where the program does not need to be interrupted for a long time.

    Disadvantages: unable to detect circular references. If the parent object has a reference to the child object, the child object in turn refers to the parent object. In this way, their reference count can never be 0.


  • Accessibility analysis method:

    The program regards all reference relationships as a graph, starting from a node GC ROOTS, looking for the corresponding reference node, after finding this node, continue to look for the reference node of this node, when all reference nodes are searched, the remaining nodes It is regarded as a node that has not been referenced, that is, a useless node, and the useless node will be judged as a recyclable object.

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

      a) Objects referenced in the virtual machine stack (local variable table in the stack frame);

      b) Objects referenced by class static properties in the method area;

      c) Methods Object referenced by constants in the area;

      d) Object referenced by JNI (Native method) in the native method stack.

Recyclable conditions:

The main contents recovered in the method area are: discarded constants and useless classes. Obsolete constants can also be judged by reference reachability, but for useless classes, the following three conditions must be met at the same time:

(1) All instances of this class have been recycled, that is, no instances of this class exist in the Java heap

(2) The ClassLoader that loaded this class has been recycled

(3) The java.lang.Class object corresponding to this class is not referenced anywhere, and the methods of this class cannot be accessed through reflection anywhere.

Common recycling algorithms:

  • Mark-Sweep algorithm

    The mark-sweep algorithm is divided into two stages: the mark stage and the clear stage. The task of the marking phase is to mark all objects that need to be recycled, and the cleaning phase is to reclaim the space occupied by the marked objects.



    Main shortcomings:


    One is the efficiency problem, the efficiency of the marking and removal process is not high.
    The other is the space problem. After the mark is cleared, a large number of discontinuous memory fragments will be generated. Too much space fragmentation may cause: When the program needs to allocate large objects in the future running process, it cannot find enough continuous memory and has to advance. Start another garbage collection action.

  • Copying algorithm

    In order to solve the shortcomings of the Mark-Sweep algorithm, the Copying algorithm was proposed. It divides the available memory into two pieces of equal size according to capacity, and only uses one piece at a time. When this block of memory is used up, copy the surviving objects to another block, and then clean up the used memory space at once, so that memory fragmentation is not easy to occur.




    Although this algorithm is simple to implement, efficient in operation and not prone to memory fragmentation, it has made a high price for the use of memory space because the memory that can be used is reduced to half of the original.

    Obviously, the efficiency of the Copying algorithm has a great relationship with the number of surviving objects. If there are many surviving objects, the efficiency of the Copying algorithm will be greatly reduced.
  • Mark-Compact algorithm

    In order to solve the defects of Copying algorithm and make full use of memory space, Mark-Compact algorithm is proposed. The marking phase of this algorithm is the same as Mark-Sweep, but after marking is completed, it does not directly clean up recyclable objects, but moves all surviving objects to one end, and then clears the memory outside the end boundary.img
  • Generational Collection (Generational Collection) algorithm

    分代收集算法是目前大部分JVM的垃圾收集器采用的算法。它的核心思想是根据对象存活的生命周期将内存划分为若干个不同的区域。一般情况下将堆区划分为老年代(Tenured Generation)和年轻代(Young Generation),在堆区之外还有一个代就是永久代(Permanet Generation),它用来存储class类、常量、方法描述等。对永久代的回收主要回收两部分内容:废弃常量和无用的类。

    老年代的特点是每次垃圾收集时只有少量对象需要被回收,而年轻代的特点是每次垃圾回收时都有大量的对象需要被回收,那么就可以根据不同代的特点采取最适合的收集算法。



    年轻代(Young Generation)的回收算法:

    在年轻代中jvm使用的是Mark-copy(标记-复制)算法

    a)所有新生成的对象首先都是放在年轻代的。年轻代的目标就是尽可能快速的收集掉那些生命周期短的对象。

    b)年轻代分三个区。一个Eden区,两个 Survivor区(一般而言)。大部分对象在Eden区中生成。当Eden区满时,还存活的对象将被复制到Survivor区(两个中的一个),当这个 Survivor区满时,此区的存活对象将被复制到另外一个Survivor区,当另外一个Survivor区也满了的时候,从第一个Survivor区复制过来的并且此时还存活的对象,将被复制到“年老区(Tenured)”。需要注意,Survivor的两个区是对称的,没先后关系,所以同一个区中可能同时存在从Eden复制过来对象,和从前一个Survivor复制过来的对象,而复制到年老区的只有从第一个Survivor区过来的对象。而且,Survivor区总有一个是空的。

    c)当survivor1区不足以存放 eden和survivor0的存活对象时,就将存活对象直接存放到老年代。若是老年代也满了就会触发一次Full GC,也就是新生代、老年代都进行回收。

    d)新生代发生的GC也叫做Minor GC,MinorGC发生频率比较高(不一定等Eden区满了才触发)。



    老年代(Old Generation)的回收算法:

    老年代的特点是每次回收都只回收少量对象,一般使用的是Mark-Compact(标记-整理)算法。

    a)在年轻代中经历了N次垃圾回收后仍然存活的对象,就会被放到年老代中。因此,可以认为年老代中存放的都是一些生命周期较长的对象。

    b)内存比新生代也大很多(大概比例是1:2),当老年代内存满时触发Major GC或Full GC,Full GC发生频率比较低,老年代对象存活时间比较长,存活率标记高。



    永久代(Permanent Generation)的回收算法:
    永久代(permanent generation)也称为“方法区(method area)”,他存储class对象和字符串常量。所以这块内存区域绝对不是永久的存放从老年代存活下来的对象的。在这块内存中有可能发生垃圾回收。发生在这里垃圾回收也被称为major GC。

常见的垃圾收集器

  • Serial Old收集器(标记-整理算法)
  • ParNew收集器(复制算法)
  • Parallel Scavenge收集器(复制算法)
  • Parallel Old收集器(标记-整理算法)
  • CMS (Concurrent Mark Sweep) collector ( mark-sweep algorithm )
  • G1

Disclaimer: This blog post is a personal study note and refers to some network resources. If there is any infringement, please let us know by private message!

Guess you like

Origin blog.csdn.net/qq_42380734/article/details/105468449