preface

Today I have a preliminary understanding of the basic knowledge of garbage recycling, listen to the leaders said that these are superficial, but still took a lot of time to understand these points

  • How do you determine whether an object is alive or not?

  • What are strong, soft, weak, and virtual references?

  • What are the garbage collection algorithms?

  • What is the generational model?

1. Two ways to determine whether an object is alive or not

Reference counting method

Add a reference counter to the object, incrementing every reference and decrement every reference. An object with a zero counter at any point in time cannot be used again.

Advantages: simple implementation, high judgment efficiency

Problem: It is difficult to solve the problem of objects referring to each other circularly

Accessibility analysis algorithm

The “GC Roots” object is used as the starting point to search down from these nodes. The path searched is called the reference chain. When an object has no reference chain connected to it, the object is not available and is judged to be recoverable.

2, strong, soft, weak, virtual four kinds of reference

Strong reference

Strong references are common in program code, such as Object obj = new Object(). As long as strong references exist, the garbage collection period will never recycle the referenced Object

Soft references

Soft references are used to describe some useful but unnecessary objects. For objects associated with soft references, the system will list these objects in the recycle range for a second recycle before the memory overflow occurs. If the recycle still does not obtain enough memory, the system will throw an overflow exception

A weak reference

Weak references are also used to describe non-essential objects, but they are weaker than soft references. Objects associated with weak references only survive until the next garbage collection occurs. When the garbage collector works, objects associated only with weak references are reclaimed regardless of whether there is currently enough memory

Phantom reference

Weak references are also used to describe non-essential objects, but they are weaker than soft references. Objects associated with weak references only survive until the next garbage collection occurs. When the garbage collector works, objects associated only with weak references are reclaimed regardless of whether there is currently enough memory

3. Garbage collection algorithm

Mark clear

The mark clearing algorithm is divided into two stages of “mark” and “clear”. First, all the objects to be recycled are marked, and all the marked objects are uniformly recycled after the marking is completed.

Applicable scenarios:

  • When the object is more alive (old age)

Disadvantages:

  • Creating a lot of discontinuous space debris,

  • Trigger GC early. Too much space debris can cause the program to be unable to find enough contiguous memory to allocate large objects during execution and trigger GC early

  • You need to scan twice, mark live objects and clear live objects

Replication algorithm

The replication algorithm divides the available memory by capacity into two equally sized parts, one at a time. When this block of memory is used up, the surviving objects are copied to another block, and the used memory space is cleaned up at once.

When objects survive for a long time, the replication algorithm needs a large number of replication operations, which reduces its efficiency. In addition, we don’t want to waste 50% of the space. In the extreme case of 100% survival of the object, we need extra space for allocation guarantee. Therefore, the replication algorithm is generally not used in the old days

Applicable scenarios:

  • Fewer living objects is more efficient
  • Suitable for young generation

Disadvantages:

  • Need memory space
  • Move objects need to be copied

Tag to sort out

The marking process is consistent with the “mark clearing” algorithm, which then moves all living objects to one end and then directly cleans up the memory outside the area of living objects.

Advantages:

  • No fragmentation is generated, facilitating object allocation
  • No memory halving

Disadvantages:

  • You need to scan it twice
  • Low efficiency

4. Recycling by generation

Today’s garbage collectors both physically and logically distinguish between these two classes of objects. We call the area occupied by objects that die fast the Young generation. The area occupied by other living long objects is called Old generation. The replication algorithm is chosen in the young generation, whereas in the old generation, because the object has a high survival rate and there is no extra space to allocate it, it must use the “mark cleaning” or “mark sorting” algorithm.

The details of generational collection and the various garbage collectors are summarized in the next article

Wechat official account: Maikezifollow to learn more about Java related knowledge. Questions or suggestions, please leave a message on the public account; There is no road in vain, as long as you walk carefully, every step counts.