golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.29k stars 17.58k forks source link

runtime: clarify statement on g object memory lifetime #65843

Open minkj1992 opened 7 months ago

minkj1992 commented 7 months ago

What is the URL of the page with the issue?

https://github.com/golang/go/blob/master/src/runtime/HACKING.md

What is your user agent?

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36

Screenshot

Screen Shot 2024-02-21 at 8 54 11 PM

What did you do?

read docs

What did you see happen?

All g, m, and p objects are heap allocated, but are never freed, so their memory remains type stable. As a result, the runtime can avoid write barriers in the depths of the scheduler.

What did you expect to see?

The statement "g objects are heap allocated, but are never freed, so their memory remains type stable" can be misleading. It may imply that the entire memory occupied by a goroutine, including its stack, remains allocated indefinitely.

To address this, proposes the following changes:

Update the documentation:

https://github.com/golang/go/blob/d4112310a4d3b1981d77226a3d52a8b566b0c0bc/src/runtime/proc.go#L4185

adonovan commented 7 months ago

The statement "g objects are heap allocated, but are never freed, so their memory remains type stable" can be misleading. It may imply that the entire memory occupied by a goroutine, including its stack, remains allocated indefinitely.

The statement is explicitly about g objects, not stacks, and the rationale (type stability) dispels any possibility that the sentence is talking about the g's stack (which is constantly changing type).

prattmic commented 7 months ago

cc @golang/runtime

prattmic commented 7 months ago

I think it would be fine to add a paragraph (here or in the "Stacks" section) describing the reuse and freeing of goroutine stacks, as that doesn't seem to be covered elsewhere. Feel free to send a CL.