gc in golang
TRANSCRIPT
GC golang
Tri-color garbage collection
•
•
• garbage
Tri-color garbage collection• root scan: root ( )
push stack
• mark phase:
• stack
• stack
•
• stack ( root root )
• sweep phase heap
Tri-color garbage collection
• mark phase & sweep phase mutator pause time
Tri-color gc
• A F push stack
root scan
• stack A F push stack
mark
• B C D stack
mark
•
sweep
mark
• mark A J
write barrier
• A I
write barrier
• sweep
write barrier
write barrier
• push stack
sweep
• < current sweep > current sweep
GOGC
GOGC
• GOGC golfing GC
• GOGC=100 heap 100% GC GC = 200 200%
tradeoff• tricolor GC pause time
• heap size sweep
• throughput CPU GC
• Spatial Locality
GC example code
test codepackage main import ( "container/list" "time" ) func GenList() { l := list.New() for i := 0; i < 10000; i++ { str := "aaaaaaaaaaaaaaaaaaa" l.PushBack(str) } } func main() { count := 0 for { count++ for i := 0; i < 1000; i++ { go func() { GenList() }() } time.Sleep(500 * time.Millisecond) if count == 360 { time.Sleep(10000 * time.Millisecond) count =0 } } }
gctrace output
• CPU 32% GC
• GC pause time 0.18 + 0.98
ms
• heap size 625M
• htop 1313M
htop
slice List
test codepackage main import ( "time" ) func GenSlice() { s:= make([]string, 10000) for i:= 0;i<10000;i++ { str := "aaaaaaaaaaaaaaaaaaa" s[i] = str } } func main() { count := 0 for { count++ for i := 0; i < 1000; i++ { go func() { GenSlice() }() } time.Sleep(500 * time.Millisecond) if count == 360 { time.Sleep(10000 * time.Millisecond) count =0 } } }
gctrace output
• CPU 5% GC
• GC pause time 1.1 + 2.1 ms
• heap size 156M
Real Memory size
• htop 188M