Golang垃圾回收
by 伊布
// The GC runs concurrently with mutator threads, is type accurate (aka precise), allows multiple // GC thread to run in parallel. It is a concurrent mark and sweep that uses a write barrier. It is // non-generational and non-compacting. Allocation is done using size segregated per P allocation // areas to minimize fragmentation while eliminating locks in the common case. //
内存屏障(英语:Memory barrier),也称内存栅栏,内存栅障,屏障指令等,是一类同步屏障指令,是CPU或编译器在对内存随机访问的操作中的一个同步点,使得此点之前的所有读写操作都执行后才可以开始执行此点之后的操作。
类型精确
多个GC线程并发Mark-sweep,写屏障
non-generational and non-compacting,无世代,无压缩?
Allocation is done using size segregated per P allocation areas to minimize fragmentation while eliminating locks in the common case.
每个Processor单独按大小隔离。避免碎片化、消除锁。
// 1. GC performs sweep termination. // // a. Stop the world. This causes all Ps to reach a GC safe-point. // // b. Sweep any unswept spans. There will only be unswept spans if // this GC cycle was forced before the expected time.
和JVM不同。达到GOGC比例的内存使用率,则触发GC
// GC rate. // Next GC is after we’ve allocated an extra amount of memory proportional to // the amount already in use. The proportion is controlled by GOGC environment variable // (100 by default). If GOGC=100 and we’re using 4M, we’ll GC again when we get to 8M // (this mark is tracked in next_gc variable). This keeps the GC cost in linear // proportion to the allocation cost. Adjusting GOGC just changes the linear constant // (and also the amount of extra memory used).
/usr/local/go/src/runtime/mgc.go:1019
每次GC必须完成如下循环,旧的完成了再开启新的。 sweep termination, mark, mark termination, and sweep
清理结束作为一个cycle的开头
// GC runs a garbage collection and blocks the caller until the // garbage collection is complete. It may also block the entire // program. func GC() { // We consider a cycle to be: sweep termination, mark, mark // termination, and sweep. This function shouldn’t return // until a full cycle has been completed, from beginning to // end. Hence, we always want to finish up the current cycle // and start a new one. That means:
Subscribe via RSS