Golang gc (garbage collection) mechanism

1. What is a GC
    program? Objects and other reference-type entities will be allocated a piece of memory space in virtual memory. If the memory space is no longer referenced by any reference variables, it will become garbage that needs to be recycled. For a program that runs for a long time, if the generated memory garbage is not recovered in time, it will cause problems such as memory leaks and even system crashes.

2. Common GC algorithms

  1. reference count
  2. mark-clear
  3. node replication
  4. Generation mobile phone

3. The gc algorithm of golang

     3.1 When to trigger gc

         When allocating objects larger than 32K bytes on the heap, check whether the garbage collection conditions are met at this time, and if so, perform garbage collection.

     3.2 Conditions to trigger gc

        The active objects on the current heap are greater than the GC trigger threshold we set during initialization (default 4M)

     3.3 The main process of garbage collection

  1. All objects are initially white
  2. From the root node of ROOT to facilitate all reachable objects, the mark will be gray and put into the pending queue.
  3. Traverse all gray object queues, mark its reference objects as gray and put them in the pending queue, and mark itself as black.
  4. After processing the gray object queue, perform cleaning work.

  4. Process flow chart analysis

 

 

Guess you like

Origin blog.csdn.net/coffiasd/article/details/114256917