GC garbage collection that Java programmers have to understand from JVM zero foundation to advanced combat

GC garbage collection that Java programmers have to understand from JVM zero foundation to advanced combat

GC garbage collection that Java programmers have to understand from JVM zero foundation to advanced combat



foreword

GC garbage collection that Java programmers have to understand from JVM zero foundation to advanced combat


GC Garbage Collection Explained

  • Why Garbage Collection?

    • A notable feature of the Java language is the introduction of a garbage collection mechanism, which solves the most troublesome memory management problem for C++ programmers. Due to a garbage collection mechanism, objects in Java no longer have the concept of "scope", only object references have "scope". Garbage collection can effectively prevent memory leaks and effectively use free memory.
  • What is the process of garbage collection?

    • Here we take the reference counting method as an example, and the approximate diagram is as follows
      Please add a picture description
  • If you were asked to consider the garbage collection algorithm, how would you design it?

    • Complete the functional requirements of which objects are recycled and which objects are not recycled
  • Whether the object is alive or not

    • Each object instance in the heap has a reference count. When an object is created and the object instance is assigned to a variable, the variable count is set to 1. When any other variable is assigned a reference to this object, the count is increased by 1 (a = b, then the counter of the object instance referenced by b is +1), but when a reference of an object instance exceeds the lifetime or is set to When a new value is obtained, the reference counter of the object instance is decremented by 1. Any object instance with a reference count of 0 can be garbage collected. When an object instance is garbage collected, the reference counters of any object instances it references are decremented by 1

Summarize

This article introduces all the content of GC garbage collection that Java programmers from JVM zero foundation to advanced combat have to understand. I will continue to update it in the future. If you like it, please click to follow. The JVM series will continue to be updated.

Guess you like

Origin blog.csdn.net/weixin_42397937/article/details/131684795