Commit 632ee88eeb for golang

commit 632ee88eebae80cf4441c9ebe66365ceb56bffc5
Author: Felix Geisendörfer <felix.geisendoerfer@datadoghq.com>
Date:   Tue Nov 24 07:19:14 2026 +0100

    runtime/pprof, net/http/pprof: improve memory profile docs

    Address post-submit review feedback on CL 841489:

    - List the predefined profiles grouped by topic, starting with the
      memory-related ones, and register them in the same order.
    - List and document the memory profile before the heap profile, since
      it's the one we recommend, end its section with that recommendation,
      and point to it from the heap profile section.
    - Drop "at the last completed GC" from the memory profile's entry in
      the list. It suggested that the heap and allocs profiles work
      differently, which they don't. The sections below explain the
      timing of each profile.
    - Note that the memory profile's breakdown frames are meant for humans
      and may change between releases.
    - Document that the memory endpoint in net/http/pprof supports the gc
      parameter, and say that it reports sampled memory usage.
    - Replace a leftover TODO on MemProfileRecord.FreedLastGC with a proper
      comment.

    For #79179

    Change-Id: I5c87a1162c74103f947b9db28ac892664c40c53d
    Reviewed-on: https://go-review.googlesource.com/c/go/+/867002
    Reviewed-by: Nick Ripley <nick.ripley@datadoghq.com>
    Reviewed-by: Michael Pratt <mpratt@google.com>
    LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
    Reviewed-by: David Chase <drchase@google.com>

diff --git a/src/internal/profilerecord/profilerecord.go b/src/internal/profilerecord/profilerecord.go
index 08ca645403..0c07e05609 100644
--- a/src/internal/profilerecord/profilerecord.go
+++ b/src/internal/profilerecord/profilerecord.go
@@ -22,8 +22,8 @@ type MemProfileRecord struct {
 	AllocObjects, FreeObjects int64

 	// FreedLastGC is the number of sampled objects from this allocation
-	// site freed in the GC cycle that produced this record.
-	// TODO: clean up/clarify
+	// site that the most recently completed GC found unreachable and
+	// freed while sweeping. The memory profile reports them as dead heap.
 	FreedLastGC int64

 	Stack []uintptr
diff --git a/src/net/http/pprof/pprof.go b/src/net/http/pprof/pprof.go
index 97755f8742..44e837b76c 100644
--- a/src/net/http/pprof/pprof.go
+++ b/src/net/http/pprof/pprof.go
@@ -33,7 +33,7 @@
 // Parameters can be passed via GET query params:
 //
 //   - debug=N (all profiles): response format: N = 0: binary (default), N > 0: plaintext
-//   - gc=N (heap profile): N > 0: run a garbage collection cycle before profiling
+//   - gc=N (heap, memory profiles): N > 0: run a garbage collection cycle before profiling
 //   - seconds=N (allocs, block, goroutine, heap, mutex, threadcreate profiles): return a delta profile
 //   - seconds=N (cpu (profile), trace profiles): profile for the given duration
 //
@@ -370,7 +370,7 @@ var profileDescriptions = map[string]string{
 	"cmdline":       "The command line invocation of the current program",
 	"goroutine":     "Stack traces of all current goroutines. Use debug=2 as a query parameter to export in the same format as an unrecovered panic.",
 	"heap":          "A sampling of memory allocations of live objects. You can specify the gc GET parameter to run GC before taking the heap sample.",
-	"memory":        "A breakdown of process memory usage at the most recently completed garbage collection.",
+	"memory":        "A breakdown of sampled process memory usage at the most recently completed garbage collection. You can specify the gc GET parameter to run GC before taking the memory sample.",
 	"mutex":         "Stack traces of holders of contended mutexes",
 	"profile":       "CPU profile. You can specify the duration in the seconds GET parameter. After you get the profile file, use the go tool pprof command to investigate the profile.",
 	"symbol":        "Maps given program counters to function names. Counters can be specified in a GET raw query or POST body, multiple counters are separated by '+'.",
diff --git a/src/runtime/pprof/pprof.go b/src/runtime/pprof/pprof.go
index b867c0ce6b..71909f19e3 100644
--- a/src/runtime/pprof/pprof.go
+++ b/src/runtime/pprof/pprof.go
@@ -105,15 +105,18 @@ import (
 //
 // Each Profile has a unique name. A few profiles are predefined:
 //
+//	memory         - a breakdown of process memory usage
+//	heap           - a sampling of memory allocations of live objects
+//	allocs         - a sampling of all past memory allocations
+//
 //	goroutine      - stack traces of all current goroutines
 //	goroutineleak  - stack traces of all leaked goroutines
-//	allocs         - a sampling of all past memory allocations
-//	heap           - a sampling of memory allocations of live objects
-//	memory         - a breakdown of process memory usage at the last completed GC
-//	threadcreate   - stack traces that led to the creation of new OS threads
+//
 //	block          - stack traces that led to blocking on synchronization primitives
 //	mutex          - stack traces of holders of contended mutexes
 //
+//	threadcreate   - stack traces that led to the creation of new OS threads
+//
 // These predefined profiles maintain themselves and panic on an explicit
 // [Profile.Add] or [Profile.Remove] method call.
 //
@@ -121,6 +124,31 @@ import (
 // the [StartCPUProfile] and [StopCPUProfile] functions, because it streams
 // output to a writer during profiling.
 //
+// # Memory profile
+//
+// The memory profile breaks down the process's resident set size (RSS) into Go
+// and non-Go memory. Go memory is further split into the heap, stacks, and
+// runtime metadata, using the runtime's memory metrics (see [runtime/metrics]).
+// The heap includes sampled live objects, as well as dead objects the most
+// recently completed garbage collection found unreachable. Both are attributed
+// to the allocation stacks of sampled objects, scaled to match the runtime's
+// heap metrics.
+//
+// The live heap portion of this profile is equivalent to the inuse_space
+// portion of the heap profile described below.
+//
+// Non-Go memory, such as memory allocated by C libraries, is estimated as RSS
+// minus Go memory and is only reported where RSS is available (currently
+// Linux and Darwin). On Linux, it is further split into file-backed and shared
+// memory, and anonymous memory.
+//
+// The breakdown is expressed as virtual frames at the root of each sample,
+// such as [Go Memory] and [Non-Go Memory]. Their names and structure are
+// intended for human consumption and may change between releases.
+//
+// For most uses, prefer this profile to the heap profile, as it contains
+// additional detail not available in the heap profile.
+//
 // # Heap profile
 //
 // The heap profile reports statistics as of the most recently completed
@@ -136,28 +164,15 @@ import (
 // flags select which to display, defaulting to -inuse_space (live objects,
 // scaled by size).
 //
+// For most uses, prefer the memory profile, which includes the same live heap
+// samples along with the rest of the process's memory usage.
+//
 // # Allocs profile
 //
 // The allocs profile is the same as the heap profile but changes the default
 // pprof display to -alloc_space, the total number of bytes allocated since
 // the program began (including garbage-collected bytes).
 //
-// # Memory profile
-//
-// The memory profile is a superset of the inuse_space heap profile. It breaks
-// down the process's resident set size (RSS) into Go and non-Go memory. Go
-// memory is further split into the heap, stacks, and runtime metadata, using
-// the runtime's memory metrics (see [runtime/metrics]). The heap includes the
-// same live object samples as the heap profile, as well as dead objects the
-// most recently completed garbage collection found unreachable. Both are
-// attributed to the allocation stacks of sampled objects, scaled to match the
-// runtime's heap metrics.
-//
-// Non-Go memory, such as memory allocated by C libraries, is estimated as RSS
-// minus Go memory and is only reported where RSS is available (currently
-// Linux and Darwin). On Linux, it is further split into file-backed and shared
-// memory, and anonymous memory. Prefer this profile to the heap profile.
-//
 // # Block profile
 //
 // The block profile tracks time spent blocked on synchronization primitives,
@@ -273,14 +288,14 @@ func lockProfiles() {
 	if profiles.m == nil {
 		// Initial built-in profiles.
 		profiles.m = map[string]*Profile{
-			"goroutine":     goroutineProfile,
-			"threadcreate":  threadcreateProfile,
+			"memory":        memoryProfile,
 			"heap":          heapProfile,
 			"allocs":        allocsProfile,
+			"goroutine":     goroutineProfile,
+			"goroutineleak": goroutineLeakProfile,
 			"block":         blockProfile,
 			"mutex":         mutexProfile,
-			"memory":        memoryProfile,
-			"goroutineleak": goroutineLeakProfile,
+			"threadcreate":  threadcreateProfile,
 		}
 	}
 }