Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runtime: huge numbers of finalizers extend STW time #18869

aclements opened this issue Jan 31, 2017 · 1 comment


None yet
3 participants
Copy link

commented Jan 31, 2017

What version of Go are you using (go version)?

go version devel +91a5f16ce2 Sun Jan 29 22:47:27 2017 -0500 linux/amd64 (roughly Go 1.8)

Currently the garbage collector scans the pending finalizers queue during mark termination. The length of this queue is under user control and also never shrinks from the maximum number of queued finalizers over the life of the process.

The STW time on my laptop is about (20ns * Q + 1ns * (M - Q)) where Q is the current number of queued finalizers and M is the maximum number of finalizers that were ever queued. These constants are quite small, but the following ridiculous example demonstrates how to use this to drive up the STW time quite high:

package main

import "runtime"

var sink interface{}

// active controls whether finalizers should be left on the queue or
// not.
const active = true

func main() {
	// Create a lot of objects with finalizers.
	objs := make([]*byte, 1000000)
	for i := range objs {
		obj := new(byte)
		runtime.SetFinalizer(obj, func(x *byte) {
			if active {
				select {}
		objs[i] = obj

	// Release all of the objects.
	objs = nil

	if !active {
		// Clear the finalizers queue.

	// Run GC.
	for {
		sink = make([]byte, 1<<20)

@aclements aclements added this to the Go1.9 milestone Jan 31, 2017

@aclements aclements self-assigned this Jan 31, 2017


This comment has been minimized.

Copy link

commented Jan 31, 2017

CL mentions this issue.

@dsnet dsnet added the Performance label Jan 31, 2017

@gopherbot gopherbot closed this in f1ba75f Mar 3, 2017

@golang golang locked and limited conversation to collaborators Mar 3, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.