mirror of
https://github.com/golang/go
synced 2024-11-23 12:20:12 -07:00
runtime: start GC background sweep eagerly
Starting it lazily causes a memory allocation (for the goroutine) during GC. First use of channels for runtime implementation. Change-Id: I9cd24dcadbbf0ee5070ee6d0ed7ea415504f316c Reviewed-on: https://go-review.googlesource.com/6960 Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Austin Clements <austin@google.com>
This commit is contained in:
parent
632217aae4
commit
5789b28525
@ -176,6 +176,16 @@ func gcinit() {
|
||||
memstats.next_gc = heapminimum
|
||||
}
|
||||
|
||||
// gcenable is called after the bulk of the runtime initialization,
|
||||
// just before we're about to start letting user code run.
|
||||
// It kicks off the background sweeper goroutine and enables GC.
|
||||
func gcenable() {
|
||||
c := make(chan int, 1)
|
||||
go bgsweep(c)
|
||||
<-c
|
||||
memstats.enablegc = true // now that runtime is initialized, GC is okay
|
||||
}
|
||||
|
||||
func setGCPercent(in int32) (out int32) {
|
||||
lock(&mheap_.lock)
|
||||
out = gcpercent
|
||||
@ -568,10 +578,7 @@ func gcSweep(mode int) {
|
||||
|
||||
// Background sweep.
|
||||
lock(&sweep.lock)
|
||||
if !sweep.started {
|
||||
go bgsweep()
|
||||
sweep.started = true
|
||||
} else if sweep.parked {
|
||||
if sweep.parked {
|
||||
sweep.parked = false
|
||||
ready(sweep.g)
|
||||
}
|
||||
|
@ -42,8 +42,14 @@ func finishsweep_m() {
|
||||
}
|
||||
}
|
||||
|
||||
func bgsweep() {
|
||||
func bgsweep(c chan int) {
|
||||
sweep.g = getg()
|
||||
|
||||
lock(&sweep.lock)
|
||||
sweep.parked = true
|
||||
c <- 1
|
||||
goparkunlock(&sweep.lock, "GC sweep wait", traceEvGoBlock)
|
||||
|
||||
for {
|
||||
for gosweepone() != ^uintptr(0) {
|
||||
sweep.nbgsweep++
|
||||
|
@ -58,7 +58,7 @@ func main() {
|
||||
}
|
||||
}()
|
||||
|
||||
memstats.enablegc = true // now that runtime is initialized, GC is okay
|
||||
gcenable()
|
||||
|
||||
if iscgo {
|
||||
if _cgo_thread_start == nil {
|
||||
|
Loading…
Reference in New Issue
Block a user