This is the 26th day of my participation in the August Text Challenge.More challenges in August

WangScaler: A writer with heart.

Declaration: uneducated, if there is a mistake, kindly correct.

GC is responsible for the smooth execution of our Java program. We know that memory is limited and that any object we create in our program consumes resources, which are released through GC garbage collection.

Determines whether an object can be reclaimed

We know that our object can be reclaimed if it is not referenced, so how do we know if an object is referenced?

  • Reference counting method: add a reference counter to the object, each time there is a reference, the counter increment one; When a reference is invalid, the counter decays by one. When the counter is zero, that is, when no reference is made, the object is reclaimed. This approach is rarely used, however, because it does not solve the cross-reference problem.
  • Reachable analysis: GC-root searches down, and objects that are not on the link will be reclaimed. Reachable objects on the link are considered alive.

Objects that can be used as gC-root are:

  • 1. Objects referenced in the virtual machine stack.
  • 2. The object referenced by the class static attribute in the method area.
  • The object referenced by the constant in the method area.
  • 4. Objects referenced by JNI(Native method) in the Native method stack.

Garbage collection algorithm

These algorithms have their own advantages and disadvantages, so they should be used in their own application scenarios, from which generation collection method comes.

  • Replication method: The space is divided into two half areas: From and To. During each reclamation, surviving objects in one half area are copied To the other half area, and then the whole half area is reclaimed.
  • Mark removal method: mark the object to be recycled first, and then unified recycling. Memory fragmentation is caused, that is, memory areas are not contiguous.
  • Mark compression: scan mark alive objects moved to one end, thereby reforming a contiguity of memory area. The cost of moving memory objects.
  • Generation collection method: The new generation (a large number of objects die, a small number of objects survive) uses the replication algorithm, the replication cost is low; In the old age (object survival rate is high, no extra space allocation guarantee), the mark-clean algorithm or mark-tidy algorithm is adopted.

Copy the method

GC as a whole is shown in the figure above, divided into Cenozoic and old age.

  • New generation: The new generation mainly stores newly created objects, with relatively small memory size and frequent garbage collection. It is divided into Eden area and two Survivor areas.
  • Old age: The old generation mainly stores objects that the JVM considers to have a long lifetime (objects that are still alive after a few GCS in the new generation).

Next, let’s look at generational collection.

1. When an object is created, it will put the object into Eden area until Eden area is full, and GC will be triggered. At this time, the surviving objects in Eden area will be copied to From area through the replication algorithm.

At this point, the remaining objects in Eden area are reclaimed.

2. When GC is triggered next time Eden block is full, objects surviving in Eden block and From block will be copied To To block. Reclaim the remaining objects in the Eden and From areas.

3. When the next Eden block is full, GC will be triggered again, and the surviving objects in Eden block and To block will be copied To From block. Then the remaining objects in Eden and To are reclaimed.

4. Repeat steps 2 and 3 above until the From area or To area reaches the threshold. Objects that are alive and drifting back and forth between the From and To zones are copied To the old age.

5. When the old age reaches the threshold, it adopts the mark-cleaning algorithm or the mark-cleaning algorithm to carry out full recovery. All threads will stop except the garbage collector thread.

Mark clearance

As shown in the figure above, red indicates objects that are alive, blue indicates objects that have references but are not reachable by GC-roots, and gray indicates objects that are no longer alive. All objects will be collected, except for the red ones that are marked and will not be collected.

Label compression

The markup compression method moves the surviving objects to the edge of memory, and then reclaims the remaining space.

conclusion

The combination of the above three methods is the generation collection method, and that’s all for today’s sharing.

Come all come, click “like” and then go!

Follow WangScaler and wish you a promotion, a raise and no bucket!