Interview Talk-Talk about your understanding of GC

Interview Talk-Talk about your understanding of GC

As a poor JAVA developer, I am often asked in interviews: What kind of rubbish are you? Oh no, do you know the garbage collection mechanism of JAVA?

Next, I will make a comprehensive summary of GC. I hope I can confidently answer the interviewer next time: I am the kind that can be recycled by your company.

Timing of GC

First of all, depending on the memory area, the JVM working mode is different, and the GC also has some differences.

The new generation (minor GC/young GC)

PS: The memory space of the new generation can be divided into 3 areas, the Eden area (the garden of Eden that produces new life), the from area and the to area (two plates used to invert objects during GC)

Minor GC will be triggered when the JVM cannot allocate space for a new object, such as when the Eden area is full. So if the program creates objects frantically, GC will be triggered frequently.

Old generation/permanent generation (major GC)/full heap (full GC)

When an object has gone through too many GCs without dying, it will break through the shackles of the old rules, soar to the old/permanent generation, and become a new existence, but it does not mean that it can not die. No one can die, especially the new one. When the place lacks resources. Major GC is usually equivalent to full GC, collecting the entire GC heap.

The object promoted to the permanent generation is larger than the remaining space full gc of the permanent generation, or is forced to full gc by the HandlePromotionFailure parameter when it is smaller.

Other GC timing:

  • Call the System.gc() function in the program, it is recommended that JVM do GC (note that it is only a recommendation, JVM has the right to ignore the recommendation of the programmer)
  • Tuning to control the age of entering the old/permanent generation (if I remember, I will write it down, I didn’t search, I’m too lazy to search)
  • Before OOM (Always make the last effort before giving up the treatment and throwing an exception)

The goal of GC-unused objects

Of course the objects that are not used, the objects that are still in use are recycled. Who knows what the program will look like?

So what are the unused objects?

Out of scope object/reference count is empty

Reference counting method: Someone quotes +1, and is discarded by others -1.

  • Advantages: Once there is no reference, the memory can be released, no need to wait for a specific time, and the recovery time is relatively even.
  • Disadvantages: maintenance count consumes resources (but not a big problem), circular references (big)

Reachability analysis, there are objects that cannot be reached by gc root

Using a series of objects called "GC Roots" as the starting point, starting from these nodes and searching downwards, the unreachable objects are useless. Objects that can be regarded as root, such as thread objects, local variables, global variables, etc.

The concept of citation is introduced here, and the order of strength from strong to weak is as follows:

  • Strong reference: The most common one is Object a = new Object(), even if OOM does not recycle
  • Soft Reference (SoftReference): Reclaim when the memory is not enough
  • Weak Reference (WeakReference): normal gc, will be reclaimed when it is scanned
  • Phantom Reference (PhantomReference): Cannot get/resurrect objects, get notified during garbage collection

GC behavior

Simply put, delete objects and reclaim space. Depending on the algorithm, the specific behavior is also different. When the battery is almost out, I will not write (mainly not prepared)

Give a chestnut:

1. Stop other threads and mark the object

2. Clean up objects 2.1. Cenozoic-copy and clean up: the surviving objects of Eden and from are copied to to, and then from and to are exchanged, the surviving object age is +1, and the age reaches the threshold to enter the old generation/permanent generation 2.2./Defragment: The mark cleaning speed is fast, but there is memory fragmentation. The mark defragmentation will move the living objects and eliminate the memory fragment