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

fmt: incorrect usage of sync.Pool #27740

Closed
dsnet opened this issue Sep 18, 2018 · 1 comment
Closed

fmt: incorrect usage of sync.Pool #27740

dsnet opened this issue Sep 18, 2018 · 1 comment

Comments

@dsnet
Copy link
Member

@dsnet dsnet commented Sep 18, 2018

Consider this snippet:

type CustomStringer int

func (c CustomStringer) String() string {
	time.Sleep(500 * time.Millisecond) // simulate processing time
	return string(make([]byte, int(c)))
}

func main() {
	processRequest := func(size int) {
		fmt.Sprintf("<%v>", CustomStringer(size))
		time.Sleep(1 * time.Millisecond) // simulate idle time
	}

	// Simulate a steady stream of infrequent large requests.
	go func() {
		for {
			processRequest(1 << 28) // 256MiB
		}
	}()

	// Simulate a storm of small requests.
	for i := 0; i < 1000; i++ {
		go func() {
			for {
				processRequest(1 << 10) // 1KiB
			}
		}()
	}

	// Continually run a GC and track the allocated bytes.
	var stats runtime.MemStats
	for i := 0; ; i++ {
		runtime.ReadMemStats(&stats)
		fmt.Printf("Cycle %d: %dB\n", i, stats.Alloc)
		time.Sleep(time.Second)
		runtime.GC()
	}
}

This is a variation of #23199 (comment). See that comment for why this memory leak occurs.

After the 50th GC cycle, this pins 7GiB of heap, when I expect it to use no more than 256MiB.

@gopherbot
Copy link

@gopherbot gopherbot commented Sep 18, 2018

Change https://golang.org/cl/136116 mentions this issue: fmt: fix usage of sync.Pool

@bcmills bcmills added this to the Go1.12 milestone Sep 19, 2018
@gopherbot gopherbot closed this in e82d152 Sep 19, 2018
@golang golang locked and limited conversation to collaborators Sep 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.