Graphical garbage algorithm, No, garbage picking algorithm

Object life and death


Today is not to introduce you to the objects , but to introduce you to garbage, because garbage will occupy memory and need to be cleaned up. Today we will talk about the way JVM recycles garbage! First of all, let's look at the life cycle of the object.

image.png

First explain a few nouns:

  • Cenozoic: rapid growth and storage of relatively young objects.
  • Old generation: store older objects.
  • Surviror: After reclaiming the new generation of memory, it contains the remaining surviving objects, which are divided into From area and to memory area.


Newly born objects are in the eden area. When the eden area is full, large objects cannot be accommodated, and GC will be triggered. If the object is still alive, the small object enters the From area or the to area. One of the two areas is empty. Suppose now The loaded object is the from area, then all objects in the from area will be copied to the to area after GC, and the from area is cleared, the age of the surviving object will increase by one year, and when the object reaches a certain age, it will enter the old age. If the object is relatively large, Surviror cannot fit, and will go directly to the old age. If the old age can't fit either, an error will be reported: heap memory overflow .

After a brief introduction to the garbage life cycle, let's take a look at the garbage cleaning algorithm.


Reference counting


How does the reference counting method determine that an object is garbage? It depends on whether there is a reference to the object. The reference counting method means that if an object has 1 reference counter is 1, and 2 reference counters are 2, if there is no reference, the counter is 0, which is a garbage object. The disadvantage is that the objects refer to each other and the objects cannot be recycled . Draw a simple case.

image.png

As shown in the code above: the teacher holds both its own reference and the student's reference. The counter is 2. The student holds its own reference and the teacher's reference. The counter is 2. This is called mutual reference, and eventually neither the teacher nor the student object can be recycled. So now the garbage collector generally does not use the reference counting method.


Mark-and-sweep


Mark-clear is divided into two steps. The GC thread first marks the reachable objects, and when GC occurs, clears the unreachable objects. The disadvantage is memory fragmentation after recovery.image.png

如上图,我们知道分配内存都是连续的,垃圾对象回收后,内存很不规则,不利于内存使用效率。垃圾对象是不可达的?那什么叫可达对象呢,什么叫不可达对象呢?

  • 可达对象:从根引用搜索,能达到的对象叫可达对象,如绿色存活对象叫可达对象。如果从根引用搜索,
  • 不可达对象:不能达到的对象叫不可达对象。如黄色部分,就是垃圾对象,特别注意:此黄非彼黄。
  • 引用:也叫GC root,存放在栈中,指向堆的引用,一般用参数或者局部变量表示。

理论性的东西还是比较难理解,我们画图表示下。

image.png

假设:new Object()对应的堆地址是0xJL。

Object object = new Object(); 栈object引用指向new Object()对应的0xJL地址,new Object()对象可达,其中object就叫做根对象

object = null; 告诉gc线程,没有引用指向0xJL了,那这块内存就有可能被标记为垃圾对象。


复制算法


复制算法需要将一块空白的内存一分为二,GC后,将可达对象全部移动到另一块内存。新生代对象朝生夕死,GC后将活着的对象移动到另一块空内存,并将当前使用的内存清空。每次GC,循环往复。

image.png

如上图:新生代采用复制算法,GC回收前使用from区域,GC后使用to内存区域。


标记整理法


标记整理算基于标记清除算法做了一定优化,gc线程首先从根节点开始标记可达对象,并将可达对象压缩到内存顶部,最后清除边界区域,老年代对象生命周期长,比较适用于标记整理算法。

image.png

As shown above: When the old age is full, Full GC will be triggered to compress and organize the memory.


to sum up


The drawing explains the meaning and shortcomings of several GC algorithms. I hope it will be helpful to you. If you like, please like and pay attention. Point of attention, do not get lost, I was [ called practice ] public number , the Micro Signal [jiaolian123abc] telling them while practicing.



Guess you like

Origin blog.51cto.com/14883474/2664455