Chapter 14 Overview of Garbage Collection

Chapter 14 Overview of Garbage Collection

JVM explained by Song Hongkang from Shang Silicon Valley: bilibili link

1 What is rubbish

Insert picture description here

Java = (C ++) -


  • Garbage collection is not a companion product of the Java language. As early as 1960, the first Lisp language that began to use dynamic memory allocation and garbage collection technology was born.
  • There are three classic questions about garbage collection:
    • What memory needs to be collected?
    • When is the garbage collected?
    • How to recycle garbage?
  • The garbage collection mechanism is Java's signature ability, which greatly improves development efficiency . Nowadays, garbage collection has almost become the standard configuration of modern languages. Even after such a long period of development, Java's garbage collection mechanism is still evolving. Devices of different sizes and application scenarios with different characteristics have put forward new ideas for garbage collection. Challenge, of course, is also a hot spot for interviews .

  • Dachang interview questions
    • Ant Financial :
      • Do you know which types of garbage collectors, their advantages and disadvantages, focus on CMS and G1.
      • One side: What are the JVM GC algorithms, and what recycling algorithms are used in the current JDK version.
      • One side: G1 collector talks about the recycling process
      • What is GC? Why do we need GC?
      • One side: two judgment methods of GC? Features of CMS collector and G1 collector.
    • Baidu :
      • Talk about the GC algorithm, let's talk about the generational collection
      • Garbage collection strategies and algorithms
    • Tmall :
      • One side: JVM GC principle, how does JVM reclaim memory?
      • One side: CMS features, what are the garbage collection algorithms? Their respective advantages and disadvantages, what are their common disadvantages?
    • Didi :
      • On the one hand: What are the garbage collectors in Java? Let's talk about the application scenarios of G1. How do you usually use garbage collectors together?
    • Jingdong :
      • Do you know which types of garbage collectors, their advantages and disadvantages, focus on CMS and G1, including principles, processes, advantages and disadvantages, the implementation principle of garbage collection algorithms.
    • Ali :
      • Talk about garbage collection algorithm
      • Under what circumstances will garbage collection be triggered?
      • How to choose a suitable garbage collection algorithm?
      • What are the three main garbage collectors for the JVM?
    • Byte bounce :
      • What are the common garbage collector algorithms, and what are the advantages and disadvantages of each?
      • What will system.gc() and runtime.gc() do?
      • One side: Java GC mechanism? What are GC Roots?
      • Two sides: the recycling method and recycling algorithm of Java objects.
      • Do CMS and G1 understand what problems CMS solves? Tell me about the recycling process.
      • CMS recovery has been paused several times. Why is it necessary to pause twice?

  • What is Garbage?
    • Garbage refers to an object that does not have any pointers in the running program . This object is garbage that needs to be recycled.
    • 外文:An object is considered garbage when it can no longer be reached from any pointer in the running program.
  • If the garbage in the memory is not cleaned up in time, the memory space occupied by these garbage objects will remain until the end of the application, and the reserved space cannot be used by other objects. It may even cause memory overflow.

2 Why do we need GC

  • For high-level languages, a basic cognition is that if garbage collection is not performed, memory will be consumed sooner or later , because memory space is constantly allocated without recycling, as if it is constantly producing household garbage and never cleaning.
  • Dealing with and releasing unused objects, garbage collection can also clear the record fragments in the memory. The defragmentation divides the occupied heap memory at one end of the heap, and the JVM allocates the defragmented memory to new objects .
  • As the business handled by the application becomes larger and more complex, and there are more and more users, the normal operation of the application cannot be guaranteed without GC . The GC of STW often fails to keep up with the actual demand, so it will continue to try to optimize the GC.

3 Early garbage collection

  • In the early C/C++ era, garbage collection was basically done manually. Developers can use the new keyword to apply, and use the delete keyword to release memory. For example, the following code:

    MibBridge *pBridge = new cmBaseGroupBridge();
    // 如果注册失败,使用delete释放该对象所占内存区域
    if (pBridge->Register(kDestroy) != NO_ERROR)
        delete pBridge;
    
  • This method can flexibly control the time of memory release, but it will bring the management burden of frequent application and release of memory to developers . If a memory area is forgotten to be recycled due to a programmer's coding problem, then a memory leak will occur , and garbage objects can never be cleared. As the system running time continues to grow, the memory consumed by garbage objects may continue to rise until memory appears. Overflow and cause the application to crash .

  • With the garbage collection mechanism, the above code is very likely to become like this:

    MibBridge *pBridge = new cmBaseGroupBridge();
    pBridge->Register(kDestroy
    
  • Now, in addition to Java, C#, Python, Ruby and other languages ​​all use the idea of ​​automatic garbage collection, which is also a future development trend. It can be said that this automatic memory allocation and garbage collection method has become a necessary standard for modern languages.

4 Java garbage collection mechanism

  • Automatic memory management eliminates the need for developers to manually participate in memory allocation and recovery, which reduces the risk of memory leaks and memory overflow .
    • Without a garbage collector, Java will be the same as cpp, all kinds of dangling pointers, wild pointers, leaking problems make you a headache.
  • The automatic memory management mechanism frees programmers from heavy memory management and can focus more on business development .
  • An introduction to garbage collection on the oracle official website: https://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/toc.html

  • Worries

    • For Java developers, automatic memory management is like a black box. If you rely too much on "automatic", then this will be an extraordinary disaster. The most serious one will weaken the problem of locating Java developers when the program has memory overflow. The ability to solve problems .
    • At this point, it is very important to understand the principles of JVM's automatic memory allocation and memory recycling. Only after we truly understand how JVM manages memory can we quickly locate and solve problems based on the error log when we encounter OutOfMemoryError.
    • When it is necessary to troubleshoot various memory overflows and memory leaks, when garbage collection is called the bottleneck of the system reaching higher concurrency, we must implement Biya's monitoring and adjustment of these "automated" technologies .
  • Which areas should be concerned about recycling?

    Insert picture description here


  • The garbage collector can collect the young generation, the old generation, and even the entire heap and method area (the Java virtual machine specification does not stipulate that the method area must be recycled).
    • Among them, the Java heap is the focus of the garbage collector .
  • In terms of frequency:
    • Frequent collection of young generation
    • Less collection of old generations
    • Basic immobile method area

Guess you like

Origin blog.csdn.net/weixin_42638946/article/details/113695963