From 75345cff8a157df5788af06f0bd3cdafe606e543 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Mon, 9 Nov 2026 09:30:20 -0800 Subject: [PATCH] Collect garbage before writing allocation profiles An allocation profile reflects the heap as of the most recently completed garbage collection, so allocations made since then are missing. At the end of a compilation the last collection can be long before the end of the run, and how much is missing varies from run to run. Profiling webpack with a 4 KiB sampling rate, the --pprofDir memory profile accounted for about 2.3M of the 5.3M objects allocated, and for 5.2M when collecting first, so comparing two builds' profiles could show the one allocating more as allocating less. SaveHeapProfile already collects first; do the same for the --pprofDir memory profile and for SaveAllocProfile. --- tsc/internal/pprof/pprof.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tsc/internal/pprof/pprof.go b/tsc/internal/pprof/pprof.go index c948eab80b0f4..888737da2855d 100644 --- a/tsc/internal/pprof/pprof.go +++ b/tsc/internal/pprof/pprof.go @@ -80,6 +80,9 @@ func (p *ProfileSession) Stop() { if err != nil { panic(err) } + // The profile reflects the heap as of the last completed collection, + // which can be long before the end of the run. + runtime.GC() if err := pprof.Lookup("allocs").WriteTo(memFile, 0); err != nil { panic(err) } @@ -180,6 +183,7 @@ func SaveAllocProfile(profileDir string) (string, error) { } defer allocFile.Close() + runtime.GC() if err := pprof.Lookup("allocs").WriteTo(allocFile, 0); err != nil { os.Remove(allocProfilePath) return "", fmt.Errorf("failed to write alloc profile: %w", err)