To get the live heap object count, use inuse_objects. This will not account for the objects already collected by the garbage collector.
Now, in the reuslt below, the preallocated and growing versions did not differ significantly. BuildMessageBatchGrowing has 2 live heap objects; BuildMessageBatchPreallocated has 1.
go test -run '^$' \
-bench '^BenchmarkBuildMessageBatch$/^implementation=growing$' \
-benchtime=100x \
-memprofilerate=1 \
-memprofile=/tmp/codec01-message-batch-growing-memory.pprof
goos: linux
goarch: arm64
pkg: codec01
BenchmarkBuildMessageBatch/implementation=growing-12 100 97617 ns/op 655.62 MB/s 285435 B/op 19 allocs/op
PASS
ok codec01 0.026s
go test -run '^$' \
-bench '^BenchmarkBuildMessageBatch$/^implementation=preallocated$' \
-benchtime=100x \
-memprofilerate=1 \
-memprofile=/tmp/codec01-message-batch-preallocated-memory.pprof
goos: linux
goarch: arm64
pkg: codec01
BenchmarkBuildMessageBatch/implementation=preallocated-12 100 23064 ns/op 2774.89 MB/s 65537 B/op 1 allocs/op
PASS
ok codec01 0.015s
go tool pprof -top -inuse_objects \
/tmp/codec01-message-batch-growing-memory.pprof
File: codec01.test
Build ID: f4a53b5a12db4d35158b288d82f85a5798e33258
Type: inuse_objects
Time: 2026-11-03 20:28:02 PST
Showing nodes accounting for 87, 100% of 87 total
flat flat% sum% cum cum%
59 67.82% 67.82% 59 67.82% runtime.mallocgc
3 3.45% 71.26% 3 3.45% sync.(*Pool).pinSlow
2 2.30% 73.56% 2 2.30% codec01.BuildMessageBatchGrowing (inline)
2 2.30% 75.86% 2 2.30% internal/sync.newIndirectNode[go.shape.interface {},go.shape.interface {}] (inline)
2 2.30% 78.16% 2 2.30% os.newFile
2 2.30% 80.46% 10 11.49% regexp.compile
2 2.30% 82.76% 2 2.30% regexp.onePassCopy
2 2.30% 85.06% 2 2.30% time.newTimer
1 1.15% 86.21% 1 1.15% fmt.(*buffer).writeString (inline)
1 1.15% 87.36% 1 1.15% fmt.init.func1
1 1.15% 88.51% 1 1.15% regexp.makeOnePass.func1
1 1.15% 89.66% 3 3.45% regexp.newOnePassMachine (inline)
1 1.15% 90.80% 1 1.15% regexp/syntax.(*Regexp).CapNames
1 1.15% 91.95% 1 1.15% regexp/syntax.(*compiler).init (inline)
1 1.15% 93.10% 1 1.15% regexp/syntax.(*compiler).inst (inline)
1 1.15% 94.25% 1 1.15% regexp/syntax.(*parser).maybeConcat
1 1.15% 95.40% 1 1.15% testing.(*B).StopTimer
1 1.15% 96.55% 1 1.15% testing.(*B).run1
1 1.15% 97.70% 3 3.45% testing.(*M).startAlarm
1 1.15% 98.85% 1 1.15% testing.rewrite
1 1.15% 100% 1 1.15% unicode/utf8.AppendRune (inline)
0 0% 100% 16 18.39% codec01.BenchmarkBuildMessageBatch
0 0% 100% 3 3.45% codec01.BenchmarkBuildMessageBatch.func1
0 0% 100% 1 1.15% fmt.(*fmt).fmtS
0 0% 100% 1 1.15% fmt.(*fmt).padString
0 0% 100% 1 1.15% fmt.(*pp).doPrintln
0 0% 100% 1 1.15% fmt.(*pp).fmtString
0 0% 100% 1 1.15% fmt.(*pp).printArg
0 0% 100% 2 2.30% fmt.Fprintf
0 0% 100% 1 1.15% fmt.Fprintln
0 0% 100% 2 2.30% fmt.newPrinter
0 0% 100% 1 1.15% internal/godebug.init.0
0 0% 100% 1 1.15% internal/godebug.setUpdate
0 0% 100% 1 1.15% internal/godebug.update
0 0% 100% 1 1.15% internal/sync.(*HashTrieMap[go.shape.interface {},go.shape.interface {}]).Delete (inline)
0 0% 100% 1 1.15% internal/sync.(*HashTrieMap[go.shape.interface {},go.shape.interface {}]).LoadAndDelete
0 0% 100% 1 1.15% internal/sync.(*HashTrieMap[go.shape.interface {},go.shape.interface {}]).Range
0 0% 100% 2 2.30% internal/sync.(*HashTrieMap[go.shape.interface {},go.shape.interface {}]).init (inline)
0 0% 100% 2 2.30% internal/sync.(*HashTrieMap[go.shape.interface {},go.shape.interface {}]).initSlow
0 0% 100% 8 9.20% main.main
0 0% 100% 2 2.30% os.Create (inline)
0 0% 100% 2 2.30% os.OpenFile
0 0% 100% 2 2.30% os.openFileNolog
0 0% 100% 3 3.45% regexp.(*Regexp).MatchString (inline)
0 0% 100% 3 3.45% regexp.(*Regexp).doExecute
0 0% 100% 3 3.45% regexp.(*Regexp).doMatch (inline)
0 0% 100% 3 3.45% regexp.(*Regexp).doOnePass
0 0% 100% 10 11.49% regexp.Compile (inline)
0 0% 100% 3 3.45% regexp.compileOnePass
0 0% 100% 1 1.15% regexp.makeOnePass
0 0% 100% 1 1.15% regexp.onePassPrefix
0 0% 100% 1 1.15% regexp/syntax.(*compiler).compile
0 0% 100% 1 1.15% regexp/syntax.(*compiler).rune
0 0% 100% 1 1.15% regexp/syntax.(*parser).literal
0 0% 100% 1 1.15% regexp/syntax.(*parser).push
0 0% 100% 2 2.30% regexp/syntax.Compile
0 0% 100% 1 1.15% regexp/syntax.Parse (inline)
0 0% 100% 1 1.15% regexp/syntax.parse
0 0% 100% 3 3.45% runtime.(*scavengerState).sleep
0 0% 100% 3 3.45% runtime.(*timer).maybeAdd
0 0% 100% 3 3.45% runtime.(*timer).modify
0 0% 100% 3 3.45% runtime.(*timer).reset (inline)
0 0% 100% 3 3.45% runtime.(*timers).addHeap
0 0% 100% 4 4.60% runtime.acquireSudog
0 0% 100% 1 1.15% runtime.allgadd
0 0% 100% 25 28.74% runtime.allocm
0 0% 100% 3 3.45% runtime.bgscavenge
0 0% 100% 1 1.15% runtime.doInit (inline)
0 0% 100% 1 1.15% runtime.doInit1
0 0% 100% 16 18.39% runtime.gcBgMarkWorker
0 0% 100% 4 4.60% runtime.gcMarkDone
0 0% 100% 6 6.90% runtime.gcStart.func4
0 0% 100% 1 1.15% runtime.godebugNotify
0 0% 100% 4 4.60% runtime.growslice
0 0% 100% 8 9.20% runtime.mProfStackInit (inline)
0 0% 100% 9 10.34% runtime.main
0 0% 100% 8 9.20% runtime.makeProfStackFP (inline)
0 0% 100% 8 9.20% runtime.makeslice
0 0% 100% 22 25.29% runtime.malg
0 0% 100% 12 13.79% runtime.mcall
0 0% 100% 16 18.39% runtime.mcommoninit
0 0% 100% 4 4.60% runtime.mpreinit (inline)
0 0% 100% 7 8.05% runtime.mstart
0 0% 100% 7 8.05% runtime.mstart0
0 0% 100% 7 8.05% runtime.mstart1
0 0% 100% 4 4.60% runtime.newMWeakPointer (inline)
0 0% 100% 25 28.74% runtime.newm
0 0% 100% 47 54.02% runtime.newobject
0 0% 100% 15 17.24% runtime.newproc.func1
0 0% 100% 15 17.24% runtime.newproc1
0 0% 100% 12 13.79% runtime.park_m
0 0% 100% 13 14.94% runtime.resetspinning
0 0% 100% 19 21.84% runtime.schedule
0 0% 100% 4 4.60% runtime.semacquire (inline)
0 0% 100% 4 4.60% runtime.semacquire1
0 0% 100% 6 6.90% runtime.startTheWorldWithSema
0 0% 100% 19 21.84% runtime.startm
0 0% 100% 21 24.14% runtime.systemstack
0 0% 100% 19 21.84% runtime.wakep
0 0% 100% 1 1.15% strings.(*Builder).WriteRune
0 0% 100% 1 1.15% sync.(*Map).Delete (inline)
0 0% 100% 1 1.15% sync.(*Map).Range (inline)
0 0% 100% 4 4.60% sync.(*Once).Do (inline)
0 0% 100% 4 4.60% sync.(*Once).doSlow
0 0% 100% 4 4.60% sync.(*Pool).Get
0 0% 100% 3 3.45% sync.(*Pool).pin
0 0% 100% 1 1.15% testing.(*B).Loop (inline)
0 0% 100% 17 19.54% testing.(*B).Run
0 0% 100% 1 1.15% testing.(*B).loopSlowPath
0 0% 100% 3 3.45% testing.(*B).run
0 0% 100% 2 2.30% testing.(*B).run.func1
0 0% 100% 19 21.84% testing.(*B).run1.func1
0 0% 100% 20 22.99% testing.(*B).runN
0 0% 100% 8 9.20% testing.(*M).Run
0 0% 100% 2 2.30% testing.(*M).after
0 0% 100% 2 2.30% testing.(*M).after.func1
0 0% 100% 2 2.30% testing.(*M).writeProfiles
0 0% 100% 1 1.15% testing.(*benchState).processBench
0 0% 100% 13 14.94% testing.(*matcher).fullName
0 0% 100% 1 1.15% testing.newMatcher
0 0% 100% 2 2.30% testing.runBenchmarks
0 0% 100% 1 1.15% testing.runBenchmarks.func1
0 0% 100% 1 1.15% testing.runTests
0 0% 100% 13 14.94% testing.simpleMatch.matches
0 0% 100% 1 1.15% testing.simpleMatch.verify
0 0% 100% 1 1.15% testing.tRunner
0 0% 100% 1 1.15% testing.tRunner.func1
0 0% 100% 1 1.15% testing.tRunner.func1.1
0 0% 100% 13 14.94% testing/internal/testdeps.TestDeps.MatchString
0 0% 100% 2 2.30% time.AfterFunc
go tool pprof -top -inuse_objects \
/tmp/codec01-message-batch-preallocated-memory.pprof
File: codec01.test
Build ID: f4a53b5a12db4d35158b288d82f85a5798e33258
Type: inuse_objects
Time: 2026-11-03 20:28:08 PST
Showing nodes accounting for 75, 100% of 75 total
flat flat% sum% cum cum%
51 68.00% 68.00% 51 68.00% runtime.mallocgc
2 2.67% 70.67% 2 2.67% os.newFile
2 2.67% 73.33% 10 13.33% regexp.compile
2 2.67% 76.00% 2 2.67% regexp.onePassCopy
2 2.67% 78.67% 2 2.67% sync.(*Pool).pinSlow
2 2.67% 81.33% 2 2.67% testing.(*B).run1
2 2.67% 84.00% 2 2.67% time.newTimer
1 1.33% 85.33% 1 1.33% codec01.BuildMessageBatchPreallocated (inline)
1 1.33% 86.67% 1 1.33% fmt.(*fmt).writePadding
1 1.33% 88.00% 1 1.33% fmt.init.func1
1 1.33% 89.33% 1 1.33% internal/sync.newIndirectNode[go.shape.interface {},go.shape.interface {}] (inline)
1 1.33% 90.67% 1 1.33% regexp.makeOnePass.func1
1 1.33% 92.00% 1 1.33% regexp/syntax.(*Regexp).CapNames
1 1.33% 93.33% 1 1.33% regexp/syntax.(*compiler).init (inline)
1 1.33% 94.67% 1 1.33% regexp/syntax.(*compiler).inst (inline)
1 1.33% 96.00% 1 1.33% regexp/syntax.(*parser).maybeConcat
1 1.33% 97.33% 3 4.00% testing.(*M).startAlarm
1 1.33% 98.67% 1 1.33% testing.rewrite
1 1.33% 100% 1 1.33% unicode/utf8.AppendRune (inline)
0 0% 100% 15 20.00% codec01.BenchmarkBuildMessageBatch
0 0% 100% 1 1.33% codec01.BenchmarkBuildMessageBatch.func2
0 0% 100% 1 1.33% fmt.(*fmt).fmtS
0 0% 100% 1 1.33% fmt.(*fmt).padString
0 0% 100% 1 1.33% fmt.(*pp).doPrintf
0 0% 100% 1 1.33% fmt.(*pp).fmtString
0 0% 100% 1 1.33% fmt.(*pp).printArg
0 0% 100% 4 5.33% fmt.Fprintf
0 0% 100% 3 4.00% fmt.newPrinter
0 0% 100% 1 1.33% internal/sync.(*HashTrieMap[go.shape.interface {},go.shape.interface {}]).Delete (inline)
0 0% 100% 1 1.33% internal/sync.(*HashTrieMap[go.shape.interface {},go.shape.interface {}]).LoadAndDelete
0 0% 100% 1 1.33% internal/sync.(*HashTrieMap[go.shape.interface {},go.shape.interface {}]).init (inline)
0 0% 100% 1 1.33% internal/sync.(*HashTrieMap[go.shape.interface {},go.shape.interface {}]).initSlow
0 0% 100% 8 10.67% main.main
0 0% 100% 2 2.67% os.Create (inline)
0 0% 100% 2 2.67% os.OpenFile
0 0% 100% 2 2.67% os.openFileNolog
0 0% 100% 10 13.33% regexp.Compile (inline)
0 0% 100% 3 4.00% regexp.compileOnePass
0 0% 100% 1 1.33% regexp.makeOnePass
0 0% 100% 1 1.33% regexp.onePassPrefix
0 0% 100% 1 1.33% regexp/syntax.(*compiler).compile
0 0% 100% 1 1.33% regexp/syntax.(*compiler).rune
0 0% 100% 1 1.33% regexp/syntax.(*parser).literal
0 0% 100% 1 1.33% regexp/syntax.(*parser).push
0 0% 100% 2 2.67% regexp/syntax.Compile
0 0% 100% 1 1.33% regexp/syntax.Parse (inline)
0 0% 100% 1 1.33% regexp/syntax.parse
0 0% 100% 1 1.33% runtime.(*scavengerState).sleep
0 0% 100% 1 1.33% runtime.(*timer).maybeAdd
0 0% 100% 1 1.33% runtime.(*timer).modify
0 0% 100% 1 1.33% runtime.(*timer).reset (inline)
0 0% 100% 1 1.33% runtime.(*timers).addHeap
0 0% 100% 4 5.33% runtime.acquireSudog
0 0% 100% 1 1.33% runtime.allgadd
0 0% 100% 19 25.33% runtime.allocm
0 0% 100% 1 1.33% runtime.bgscavenge
0 0% 100% 16 21.33% runtime.gcBgMarkWorker
0 0% 100% 4 5.33% runtime.gcMarkDone
0 0% 100% 6 8.00% runtime.gcStart.func4
0 0% 100% 2 2.67% runtime.growslice
0 0% 100% 6 8.00% runtime.mProfStackInit (inline)
0 0% 100% 8 10.67% runtime.main
0 0% 100% 6 8.00% runtime.makeProfStackFP (inline)
0 0% 100% 6 8.00% runtime.makeslice
0 0% 100% 20 26.67% runtime.malg
0 0% 100% 6 8.00% runtime.mcall
0 0% 100% 12 16.00% runtime.mcommoninit
0 0% 100% 3 4.00% runtime.mpreinit (inline)
0 0% 100% 7 9.33% runtime.mstart
0 0% 100% 7 9.33% runtime.mstart0
0 0% 100% 7 9.33% runtime.mstart1
0 0% 100% 3 4.00% runtime.newMWeakPointer (inline)
0 0% 100% 19 25.33% runtime.newm
0 0% 100% 43 57.33% runtime.newobject
0 0% 100% 15 20.00% runtime.newproc.func1
0 0% 100% 15 20.00% runtime.newproc1
0 0% 100% 6 8.00% runtime.park_m
0 0% 100% 7 9.33% runtime.resetspinning
0 0% 100% 13 17.33% runtime.schedule
0 0% 100% 4 5.33% runtime.semacquire (inline)
0 0% 100% 4 5.33% runtime.semacquire1
0 0% 100% 6 8.00% runtime.startTheWorldWithSema
0 0% 100% 13 17.33% runtime.startm
0 0% 100% 21 28.00% runtime.systemstack
0 0% 100% 13 17.33% runtime.wakep
0 0% 100% 1 1.33% strings.(*Builder).WriteRune
0 0% 100% 1 1.33% sync.(*Map).Delete (inline)
0 0% 100% 5 6.67% sync.(*Once).Do (inline)
0 0% 100% 5 6.67% sync.(*Once).doSlow
0 0% 100% 3 4.00% sync.(*Pool).Get
0 0% 100% 2 2.67% sync.(*Pool).pin
0 0% 100% 16 21.33% testing.(*B).Run
0 0% 100% 4 5.33% testing.(*B).run
0 0% 100% 3 4.00% testing.(*B).run.func1
0 0% 100% 16 21.33% testing.(*B).run1.func1
0 0% 100% 17 22.67% testing.(*B).runN
0 0% 100% 8 10.67% testing.(*M).Run
0 0% 100% 2 2.67% testing.(*M).after
0 0% 100% 2 2.67% testing.(*M).after.func1
0 0% 100% 2 2.67% testing.(*M).writeProfiles
0 0% 100% 1 1.33% testing.(*benchState).processBench
0 0% 100% 10 13.33% testing.(*matcher).fullName
0 0% 100% 1 1.33% testing.newMatcher
0 0% 100% 2 2.67% testing.runBenchmarks
0 0% 100% 1 1.33% testing.runBenchmarks.func1
0 0% 100% 1 1.33% testing.runTests
0 0% 100% 10 13.33% testing.simpleMatch.matches
0 0% 100% 1 1.33% testing.simpleMatch.verify
0 0% 100% 1 1.33% testing.tRunner
0 0% 100% 1 1.33% testing.tRunner.func1
0 0% 100% 1 1.33% testing.tRunner.func1.1
0 0% 100% 10 13.33% testing/internal/testdeps.TestDeps.MatchString
0 0% 100% 2 2.67% time.AfterFunc