1. JAVA GC Overview

The JAVA GC takes the generational approach of dividing the JAVA heap into new generation, old generation, and permanent generation. GC algorithms mainly include mark-clearing, mark-compression and copy algorithms.

  • New Generation: The New generation is divided into three parts: Eden region and two survivor regions (from and to). When an object is created and the JVM needs to allocate memory, it will find a suitable memory area in the Eden area of the new generation. A Minor GC is triggered when the Eden region is out of memory. The Eden live objects and the FROM live objects will be copied to the TO. When an object in the to zone ages beyond the age set for promotion, the object is promoted to the old age. The new generation GC uses the replication algorithm
  • Aged generation: The aged generation stores large objects with a long life time, so the aged generation uses the tag collation algorithm. When the old generation is full, a Major GC (Full GC) is triggered to reclaim object resources that are no longer used in the old and young generations. The old generation algorithm uses mark-clear
  • Permanent generation: Refers to the permanent storage area of memory, mainly storing Class and Meta information. The Class is put into the permanent region when it is loaded. Unlike the area where the instance is stored, the GC does not clean up the permanent area while the main program is running. This also causes the permanent generation area to swell as more classes are loaded, resulting in an OOM exception.

2. Graphic description of mark-clear and mark-compress

Step 1: Marking

The first step in GC is called tagging. In this step, GC iterates through the memory area to identify what is in use and what is not. And mark it

In the figure above, blue objects represent living objects and gold objects represent garbage objects. In the marking phase, objects in the entire memory area need to be scanned and marked. This process can be time-consuming

Step 2: Clear the Deletion (Normal Deletion).

The cleanup phase removes garbage objects and maintains free areas with a linked list

The memory allocator holds references to free memory areas to allocate memory to new objects

Step 2A: Deletion with Compacting

In order to improve performance, on the basis of Step 2, after deleting garbage objects, We can move living objects to the head of the memory area. This will make memory allocation faster the next time. The main reason is that mark-clear causes large memory fragmentation and requires traversing the free linked list whenever memory needs to be allocated. Compression algorithms, on the other hand, defragment memory into a large, complete block of memory.

3. Generational garbage collection

3.1 Why should generational garbage collection be adopted?

As mentioned earlier, marking and compressing objects can be time consuming for the Java virtual machine. As the Java virtual machine allocates more and more objects. The GC will take longer. However, based on empirical analysis, the vast majority of objects are short-lived. This way we can isolate the long-lived objects from the short-lived objects. This makes the GC more efficient

3.2 the JVM generation

Splitting THE JVM heap memory into smaller areas of memory improves THE JVM’s GC performance. The heap is divided into the Young Generation, the Old or Tenured Generation, and the Permanent Generation

  • All objects are allocated memory in the new generation. When the new generation runs out of memory. The Minor GC will start. If objects in the new generation are short-lived, the Minor GC will be efficient. If the new generation is full of garbage objects, the collection will be fast (because the marking time is short). Some of the objects that survive will age up and eventually be moved to the old generation
  • Stop the World Event: All minor GC’s are “Stop the World” events. This means that other thread pairs of the program other than the GC thread will pause until the GC completes. The Minor GC stops the World Event
  • Tenured generation is used to store objects with long lifetime. Typically we can set an age limit for objects, and when the new generation of objects live beyond this age limit, objects will move from the new generation to the old generation. When the old generation runs out of memory, the Major GC will be started. The Major GC also stops the World Event. In general, Major GC is slower than Minor GC because it collects all garbage objects from the entire generation and the entire generation. Therefore, for highly responsive programs, Major GC should be minimized. It should be noted that the time to Stop the World Event is affected by the garbage collector used in the old generation, not by the new generation
  • The persistent generation contains metadata for the JVM. Including class information, method information and so on. Persistent generations are generated by the JVM at application runtime. Class information from the Javase class library may also be stored here

3.3 The processing process of generational garbage recovery

Now that you understand why the heap is divided into different generations, it’s time to look at how these Spaces interact. The following images illustrate the object allocation and aging process in the JVM.

1. At the beginning, any new object will allocate memory in Eden space, and both survivor Spaces will be empty at the beginning

2. When the Eden space is full, the Minor GC will fire

1 and 3 in the S0 survivor area object indicate the age of the object

3. Surviving objects in Eden space will be copied to the first survivor space, age +1, and garbage objects will be removed

4. On the next minor GC, the surviving objects in Eden space are copied to S1(age +1), the free survivor space, and the surviving objects in the previous minor GC S0 space are copied to S1(age +1), and garbage objects are cleared

5. The next time a minor GC occurs, the contents of item 4 are repeated, except that the two survivor Spaces are swapped, this time copying from S1 to S0

6. The new generation ascends to the old. When a MINOR GC occurs, if an object in a survivor space is older than the promotion age limit, the object is copied to the aged generation

7. As the Minor GC continues to fire, objects will continue to be promoted to the tenor generation

8. The graphic above perfectly explains the minor GC process. Eventually, the Major GC will be triggered when the memory of the older generation is full. The Major GC uses the tag compression algorithm in the old generation. At the same time, the new generation of objects will be eliminated

Recommended reading

Event distribution tetralogy


1.1. Event Distribution One of the four steps describes event distribution in depth

1.2. Event Distribution Tetralogy 2 explains the Android nesting sliding mechanism in detail

1.3. Event Distribution The third of the tetralogy explains in detail the CoordinatorLayout event distribution mechanism

1.4. Event Distribution Tetralogy 4 describes the BottomSheetBehavior event distribution mechanism in detail

RecyclerView related


2.1. RecyclerView Recycling and reuse mechanism when rolling

2.2. Record a pit encountered by the waterfall flow layout

2.3. Explain one of the realization principles of RecyclerView animation in detail

2.4. Explain the realization principle of RecyclerView animation in detail

2.5. Talk about RecyclerView cache mechanism

The Material Design related


3.1. AppBarLayout is used to realize the second top drawing function of JD classification page

3.2. Understand AppBarLayout from these aspects

3.3. Understand BottomSheetBehavior

Open source library related


4.1. I open source a RecyclerView roof library

4.2. ViewCompat offsetTopAndBottom overturned on the scene

Welcome to pay attention to my public number: byte station


This is a public account to share Android technology dry goods, if you think my article can let you learn something, I hope you can help me share more people. If you have a good article, welcome to contribute, let us share together.