Why is GC needed?

Without garbage collection, memory will run out sooner or later. Because we are constantly allocating memory and not doing garbage collection. Unless the memory is large enough to allow us to allocate memory arbitrarily. But that’s not the case.

What is garbage?

Garbage refers to objects in memory that are no longer useful. So how do we find these useless objects. An algorithm called feasibility analysis is used in the JVM to determine whether an object should be reclaimed.

Feasibility analysis

The idea of this algorithm is to search down from a series of objects called “GCRoot” as the starting point. The path of the search is called the reference chain. When there is no reference chain from an object to GCRoot (that is, GCRoots to the object is unreachable), the object is proved to be unavailable.

As shown in the figure above, objects A, B, C, D, and E all have A direct or indirect reference chain with GCRoot, which also means that they are reachable to GCRoot and therefore cannot be reclaimed by GC. Objects M and K are referred to by J, but there is no reference chain connecting them to GCRoot, so when GC does garbage collection, it just iterates through J, K, and M and recycles them.

Note: Although the circle icon in the figure above marks an object, it actually represents an in-memory reference to that object. Including GC Root is also a set of references rather than objects. ‘

What is a GCRoot object?

1. Objects referenced by the virtual machine stack (local variables) 2. Objects referenced by constants in the method area 3

What time is GC?

1. Failed to allocate object memory due to insufficient available memory during heap memory allocation. System active GC. 2. In the application layer, the developer executes system.gc () to actively perform GC operations.

How is garbage collection done?

Garbage collection is mainly carried out in the following ways: 1. Mark clearing algorithm 2. Copy algorithm 3

Mark clearing algorithm

Tag cleanup is the most basic GC collection mechanism, literally, a two-step tag cleanup algorithm. Flags: Find all objects in memory that are connected/indirectly connected to GCRoot and mark them gray (live objects), otherwise black (junk objects).

Clear: Removes garbage objects directly.

  • Advantages: Simple implementation, no need to move the object.
  • Disadvantages: A large number of discrete memory fragments will be generated after the flag is cleared. Too many memory fragments may cause the program to be unable to find enough contiguous memory and have to trigger a garbage collection action in advance when large objects need to be allocated.

Replication algorithm

Divide the existing memory into two pieces, use one at a time, and when GC is needed, find the objects in use in memory, copy them to another piece of memory, and then empty the current memory. Swap two memory roles. Complete the GC.

  • Advantages: Allocate memory sequentially, without considering memory fragmentation.
  • Disadvantages: The available memory is reduced by half, and there are frequent GC operations.

Mark compression algorithm

The process is similar to tag clearing, but instead of clearing directly, all objects are moved to one end of memory, and those that are not available are reclaimed.

  • Advantages: This reclamation algorithm not only avoids memory fragmentation, but also does not need to divide two memory Spaces, which is the optimal solution compared with the above two.
  • Disadvantages: the so-called compression, or need to carry out memory movement, to a certain extent, reduce efficiency.

Generation collection algorithm

The JVM divides heap memory into new generation, old generation, and persistent generation based on the memory object lifetime. The central idea of generational collection is that memory is allocated in the new generation for newly created objects, which generally have a short lifetime. If they survive multiple cycles of recycling, they are transferred to older age.

The new generation

A large number of dead objects, a small number of living objects, a GC can recover 70%-95% of the space, recycling efficiency is very high. Generally, the replication algorithm is used and the replication cost is low.

The Cenozoic can be divided into one Eden region and two survivor regions. The three sections divide the new generation by eight to one.

Most new objects are placed in Eden. As shown in figure:

When Eden is full for the first time, the memory is reclaimed. Clean up the garbage objects in Eden and put the live objects to S0. S1 is empty. As shown in figure:

The next time Eden is full, remove all garbage objects in Eden and S0 and put the surviving objects in S1. Then S0 becomes empty. As shown in figure:

This is done repeatedly in S0 and S1 (default 15 times), and objects that are still alive are put to the old age.

The old s

The object has a high survival rate, no extra space is allocated, and the tag clearing or tag collation algorithm is used.

An object that has not been cleared for too long in the new generation is moved to the old age. The old generation will have much more memory space than the new generation. If an object has too much memory to fit into the new generation, the object is put directly into the old generation.

The permanent generation

Data that is permanently stored in memory, mainly class and meta information.

Four types of references

We determine whether an object is alive by reference reachability of GCRoots. But there isn’t just one kind of reference relationship in the JVM, there are four, and depending on the strength of the reference, They are StrongReference, Soft Reference, Weak Reference, and Phantom Reference.

Strong reference StrongReference

If an object has a strong reference, the garbage collector will never reclaim it, preferring to throw an OOM error when out of memory, causing the program to stop abnormally.

Object object = new Object(); Is a strong reference.

Soft reference SoftReference

If an object has only soft references, the garbage collector will not reclaim it when there is enough memory, and will reclaim objects when there is not. After a soft reference object is reclaimed, the Java virtual machine adds the soft reference to the reference queue associated with it.

A weak reference WeakReference

If an object has only weak references, the garbage collector reclaims the object’s memory when it scans it, whether it has enough memory or not. As with soft references, when a weak reference object is reclaimed, the Java virtual machine adds the weak reference to the reference queue associated with it.

Phantom reference PhantomReference

Virtual references do not determine the object life cycle. If an object has only virtual references, it can be reclaimed at any time, just as if it had no references at all. Virtual references are mainly used to track the activity of objects being collected by the garbage collector. Unlike soft and weak references, virtual references must be associated with a reference queue.

reference

Pull education – Android engineer advanced 34 speak

This article first appeared on my personal blog: GC Collection Mechanisms

More articles please pay attention to my public number: code nong workplace