垃圾回收器标记的三个工作阶段
  • mark setup(stw)
    因为并发标记过程中,业务goroutine可以继续运行,所以需要打开写屏障,打开写屏障需要goroutine进入安全点,进入安全点的唯一方法是让goroutine在函数调用的地方停止
  • mark concurrent
    gc goroutine和业务go routine公共运行,其中可能会因为业务goroutine需要分配内存的速度大于标记的速度,这时会要求改goroutine协助gc routine标记,协助的时间取决余他在堆中分配的内存大小,如果需要被要求协助的go数据很多,那么会提前开始下一次的gc
  • mark terminal(stw)
    关闭写屏障,重新计算需要标记的目标
垃圾回收器清理的过程
  • 当应用程序 Goroutine 尝试在堆内存中分配新值时,会发生此活动。Sweeping 的延迟被添加到在堆内存中执行分配的成本中,并且与任何与垃圾收集相关的延迟无关。
    回收内存的示例
    Figure 9 shows the end of the stack trace for one of the Goroutines in the Sweep activity. The call to runtime.mallocgc is the call to allocate a new value in heap memory. The call to runtime.(*mcache).nextFree is causing the Sweep activity. Once there are no more allocations in heap memory to reclaim, the call to nextFree won’t be seen any longer.
GC Percentage

默认100%,假设上一次gc后存活的内存大小为2M,在至少2M的内存被分配到堆之前必须再次进行gc

参考文章