Skip to content

Documentation: some math in the Garbage Collection guide is incorrect #67283

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

Closed
MikeMitchellWebDev opened this issue May 10, 2024 · 4 comments
Closed
Labels
Documentation Issues describing a change to documentation. FrozenDueToAge

Comments

@MikeMitchellWebDev
Copy link
Contributor

Go version

1.22

Output of go env in your module/workspace:

n/a

What did you do?

I double checked the math in this blog post about the garbage collector and it seems incorrect. https://tip.golang.org/doc/gc-guide

Meanwhile, target heap memory is defined as:

Target heap memory = Live heap + (Live heap + GC roots) * GOGC / 100

As an example, consider a Go program with a live heap size of 8 MiB, 1 MiB of goroutine stacks, and 1 MiB of pointers in global variables. Then, with a GOGC value of 100, the amount of new memory that will be allocated before the next GC runs will be 10 MiB, or 100% of the 10 MiB of work, for a total heap footprint of 18 MiB. With a GOGC value of 50, then it'll be 50%, or 5 MiB. With a GOGC value of 200, it'll be 200%, or 20 MiB.

What did you see happen?

I see two out of three equations in the paragraph as being incorrect.

Correct

*8 + (8+1+1)100/100 = 18

**Incorrect

8 + (8+1+1)*200/100 = 20

8 + (8+1+1)50/100 = 5*

What did you expect to see?

13_for_50_gogc
28_for_200_gogc
gogc_is_1_18mib
I attached some screenshots from the online calculator. When GOGC is 50, with the given numbers, the expected total should be 13 MiB (not 5). When GOGC is 200, with the given numbers, the expected total should be 28 MiB (not 20).

@gopherbot gopherbot added the Documentation Issues describing a change to documentation. label May 10, 2024
@zigo101
Copy link

zigo101 commented May 10, 2024

Those numbers are the amount of new memory, a.k.a. the (Live heap + GC roots) * GOGC / 100 part.

@MikeMitchellWebDev
Copy link
Contributor Author

MikeMitchellWebDev commented May 10, 2024

Those numbers are the amount of new memory, a.k.a. the (Live heap + GC roots) * GOGC / 100 part.

Your sentence ("those numbers") is a little bit vague. What numbers exactly? I ask that because you only quote part of the equation in question. Did you deliberately omit the first "Live heap" from the equation?

[ Target heap memory = Live heap + (Live heap + GC roots) * GOGC / 100]

Either way, I think I disagree with you, because the math is correct for one of the three equations.

Your remark seems to imply that I misunderstand the whole principle of the equation. However, if, as you seem to suggest, I misunderstood the terminology (and all the math was in fact correct), then how is it possible that I report one of the three equations as correct? Can you clarify?

@zigo101
Copy link

zigo101 commented May 10, 2024

It clearly says that 18 is total and others are new.

@cherrymui
Copy link
Member

The three numbers are

Then, with a GOGC value of 100, the amount of new memory that will be allocated before the next GC runs will be 10 MiB
With a GOGC value of 50, then it'll be 50%, or 5 MiB.
With a GOGC value of 200, it'll be 200%, or 20 MiB.

which match your numbers minus 8.

I don't see an issue here. Closing. Thanks.

@cherrymui cherrymui closed this as not planned Won't fix, can't repro, duplicate, stale May 10, 2024
@golang golang locked and limited conversation to collaborators May 10, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Documentation Issues describing a change to documentation. FrozenDueToAge
Projects
None yet
Development

No branches or pull requests

4 participants