Yesterday, we looked into alloc_space type, which accounts for all total allocated heap memory space. Now, we used alloc_objects, which accounts for the total allocation count.
The profiles are specified with 100 operations. With the “growing” function, there is a total of 1900 allocation count for 100 events for BuildMessageBatchGrowing, meaning there is 19 allocations for each operation. On the other hand, the “preallocated” function has a total of 100 total allocation count for 100 events, meaning there is exactly one allocation per event.
This memory profile tells us that the higher the number of total allocation count, the higher the size of the total allocated space.
go tool pprof -top -alloc_objects \
/tmp/codec01-message-batch-growing-memory.pprof
File: codec01.test
Build ID: a94b6e5f69aad830296a1e71a08c11623fd119cb
Type: alloc_objects
Time: 2026-10-31 21:13:06 CST
Showing nodes accounting for 2424, 97.19% of 2494 total
Dropped 95 nodes (cum <= 12)
flat flat% sum% cum cum%
1900 76.18% 76.18% 1900 76.18% codec01.BuildMessageBatchGrowing (inline)
152 6.09% 82.28% 152 6.09% regexp.makeOnePass.func1
58 2.33% 84.60% 58 2.33% runtime.mallocgc
39 1.56% 86.17% 39 1.56% regexp/syntax.(*compiler).inst (inline)
27 1.08% 87.25% 27 1.08% regexp/syntax.(*parser).newRegexp (inline)
24 0.96% 88.21% 24 0.96% regexp.newQueue (inline)
24 0.96% 89.17% 24 0.96% regexp/syntax.(*parser).maybeConcat
20 0.8% 89.98% 44 1.76% regexp/syntax.(*parser).push
16 0.64% 90.62% 16 0.64% testing.rewrite
16 0.64% 91.26% 16 0.64% unicode/utf8.AppendRune (inline)
14 0.56% 91.82% 14 0.56% regexp.onePassCopy
13 0.52% 92.34% 376 15.08% regexp.compile
13 0.52% 92.86% 189 7.58% regexp.makeOnePass
13 0.52% 93.38% 14 0.56% regexp/syntax.(*parser).collapse
13 0.52% 93.91% 13 0.52% strings.genSplit
13 0.52% 94.43% 2170 87.01% testing.(*B).runN
12 0.48% 94.91% 12 0.48% sync.(*Pool).pinSlow
12 0.48% 95.39% 12 0.48% testing.(*B).ResetTimer
9 0.36% 95.75% 164 6.58% testing.newMatcher
7 0.28% 96.03% 14 0.56% regexp/syntax.(*compiler).init (inline)
7 0.28% 96.31% 91 3.65% regexp/syntax.parse
6 0.24% 96.55% 234 9.38% testing.(*B).Run
4 0.16% 96.71% 238 9.54% testing.runBenchmarks
3 0.12% 96.83% 230 9.22% codec01.BenchmarkBuildMessageBatch
3 0.12% 96.95% 293 11.75% testing.(*M).Run
3 0.12% 97.07% 15 0.6% testing.(*benchState).processBench
3 0.12% 97.19% 40 1.60% testing.runTests
0 0% 97.19% 1901 76.22% codec01.BenchmarkBuildMessageBatch.func1
0 0% 97.19% 293 11.75% main.main
0 0% 97.19% 376 15.08% regexp.Compile (inline)
0 0% 97.19% 203 8.14% regexp.compileOnePass
0 0% 97.19% 16 0.64% regexp.onePassPrefix
0 0% 97.19% 32 1.28% regexp/syntax.(*compiler).compile
0 0% 97.19% 24 0.96% regexp/syntax.(*compiler).rune
0 0% 97.19% 14 0.56% regexp/syntax.(*parser).concat
0 0% 97.19% 54 2.17% regexp/syntax.(*parser).literal
0 0% 97.19% 16 0.64% regexp/syntax.(*parser).op
0 0% 97.19% 46 1.84% regexp/syntax.Compile
0 0% 97.19% 91 3.65% regexp/syntax.Parse (inline)
0 0% 97.19% 25 1.00% runtime.allocm
0 0% 97.19% 15 0.6% runtime.gcBgMarkWorker
0 0% 97.19% 293 11.75% runtime.main
0 0% 97.19% 22 0.88% runtime.malg
0 0% 97.19% 12 0.48% runtime.mcall
0 0% 97.19% 16 0.64% runtime.mcommoninit
0 0% 97.19% 25 1.00% runtime.newm
0 0% 97.19% 46 1.84% runtime.newobject
0 0% 97.19% 16 0.64% runtime.newproc.func1
0 0% 97.19% 16 0.64% runtime.newproc1
0 0% 97.19% 12 0.48% runtime.park_m
0 0% 97.19% 13 0.52% runtime.resetspinning
0 0% 97.19% 19 0.76% runtime.schedule
0 0% 97.19% 19 0.76% runtime.startm
0 0% 97.19% 22 0.88% runtime.systemstack
0 0% 97.19% 19 0.76% runtime.wakep
0 0% 97.19% 16 0.64% strings.(*Builder).WriteRune
0 0% 97.19% 13 0.52% strings.Split (inline)
0 0% 97.19% 12 0.48% sync.(*Once).Do (inline)
0 0% 97.19% 12 0.48% sync.(*Once).doSlow
0 0% 97.19% 14 0.56% sync.(*Pool).Get
0 0% 97.19% 12 0.48% sync.(*Pool).pin
0 0% 97.19% 24 0.96% testing.(*B).run
0 0% 97.19% 2143 85.93% testing.(*B).run1.func1
0 0% 97.19% 271 10.87% testing.(*matcher).fullName
0 0% 97.19% 248 9.94% testing.simpleMatch.matches
0 0% 97.19% 148 5.93% testing.simpleMatch.verify
0 0% 97.19% 387 15.52% testing/internal/testdeps.TestDeps.MatchString
go tool pprof -top -alloc_objects \
/tmp/codec01-message-batch-preallocated-memory.pprof
File: codec01.test
Build ID: a94b6e5f69aad830296a1e71a08c11623fd119cb
Type: alloc_objects
Time: 2026-10-31 21:13:08 CST
Showing nodes accounting for 675, 96.98% of 696 total
Dropped 42 nodes (cum <= 3)
flat flat% sum% cum cum%
167 23.99% 23.99% 167 23.99% regexp.makeOnePass.func1
100 14.37% 38.36% 100 14.37% codec01.BuildMessageBatchPreallocated (inline)
49 7.04% 45.40% 49 7.04% runtime.mallocgc
39 5.60% 51.01% 39 5.60% regexp/syntax.(*compiler).inst (inline)
27 3.88% 54.89% 27 3.88% regexp/syntax.(*parser).newRegexp (inline)
24 3.45% 58.33% 24 3.45% regexp.newQueue (inline)
24 3.45% 61.78% 24 3.45% regexp/syntax.(*parser).maybeConcat
20 2.87% 64.66% 44 6.32% regexp/syntax.(*parser).push
16 2.30% 66.95% 16 2.30% testing.rewrite
16 2.30% 69.25% 16 2.30% unicode/utf8.AppendRune (inline)
14 2.01% 71.26% 14 2.01% regexp.onePassCopy
13 1.87% 73.13% 391 56.18% regexp.compile
13 1.87% 75.00% 204 29.31% regexp.makeOnePass
13 1.87% 76.87% 14 2.01% regexp/syntax.(*parser).collapse
13 1.87% 78.74% 13 1.87% strings.genSplit
13 1.87% 80.60% 376 54.02% testing.(*B).runN
12 1.72% 82.33% 12 1.72% testing.(*B).ResetTimer
10 1.44% 83.76% 10 1.44% sync.(*Pool).pinSlow
9 1.29% 85.06% 169 24.28% testing.newMatcher
7 1.01% 86.06% 7 1.01% regexp/syntax.(*Regexp).CapNames
7 1.01% 87.07% 14 2.01% regexp/syntax.(*compiler).init (inline)
7 1.01% 88.07% 91 13.07% regexp/syntax.parse
7 1.01% 89.08% 7 1.01% testing.splitRegexp
6 0.86% 89.94% 241 34.63% testing.(*B).Run
5 0.72% 90.66% 5 0.72% fmt.(*buffer).writeString (inline)
4 0.57% 91.24% 8 1.15% context.WithCancel
4 0.57% 91.81% 4 0.57% context.withCancel (inline)
4 0.57% 92.39% 9 1.29% fmt.Sprintf
4 0.57% 92.96% 243 34.91% testing.runBenchmarks
3 0.43% 93.39% 237 34.05% codec01.BenchmarkBuildMessageBatch
3 0.43% 93.82% 3 0.43% testing.(*B).run1
3 0.43% 94.25% 298 42.82% testing.(*M).Run
3 0.43% 94.68% 15 2.16% testing.(*benchState).processBench
3 0.43% 95.11% 3 0.43% testing.(*matcher).unique
3 0.43% 95.55% 40 5.75% testing.runTests
2 0.29% 95.83% 8 1.15% regexp.newOnePassMachine (inline)
2 0.29% 96.12% 9 1.29% testing.(*B).run.func1
2 0.29% 96.41% 10 1.44% testing.benchmarkName (inline)
2 0.29% 96.70% 3 0.43% testing.prettyPrint
1 0.14% 96.84% 3 0.43% testing.(*M).startAlarm
1 0.14% 96.98% 6 0.86% testing.BenchmarkResult.String
0 0% 96.98% 100 14.37% codec01.BenchmarkBuildMessageBatch.func2
0 0% 96.98% 4 0.57% fmt.(*fmt).fmtS
0 0% 96.98% 4 0.57% fmt.(*fmt).padString
0 0% 96.98% 5 0.72% fmt.(*pp).doPrintf
0 0% 96.98% 4 0.57% fmt.(*pp).fmtString
0 0% 96.98% 4 0.57% fmt.(*pp).printArg
0 0% 96.98% 8 1.15% fmt.Fprintf
0 0% 96.98% 6 0.86% fmt.newPrinter
0 0% 96.98% 298 42.82% main.main
0 0% 96.98% 3 0.43% os.Create (inline)
0 0% 96.98% 3 0.43% os.OpenFile
0 0% 96.98% 3 0.43% os.openFileNolog
0 0% 96.98% 8 1.15% regexp.(*Regexp).MatchString (inline)
0 0% 96.98% 8 1.15% regexp.(*Regexp).doExecute
0 0% 96.98% 8 1.15% regexp.(*Regexp).doMatch (inline)
0 0% 96.98% 8 1.15% regexp.(*Regexp).doOnePass
0 0% 96.98% 391 56.18% regexp.Compile (inline)
0 0% 96.98% 218 31.32% regexp.compileOnePass
0 0% 96.98% 16 2.30% regexp.onePassPrefix
0 0% 96.98% 32 4.60% regexp/syntax.(*compiler).compile
0 0% 96.98% 8 1.15% regexp/syntax.(*compiler).empty (inline)
0 0% 96.98% 24 3.45% regexp/syntax.(*compiler).rune
0 0% 96.98% 14 2.01% regexp/syntax.(*parser).concat
0 0% 96.98% 54 7.76% regexp/syntax.(*parser).literal
0 0% 96.98% 16 2.30% regexp/syntax.(*parser).op
0 0% 96.98% 46 6.61% regexp/syntax.Compile
0 0% 96.98% 91 13.07% regexp/syntax.Parse (inline)
0 0% 96.98% 19 2.73% runtime.allocm
0 0% 96.98% 14 2.01% runtime.gcBgMarkWorker
0 0% 96.98% 6 0.86% runtime.gcStart.func4
0 0% 96.98% 6 0.86% runtime.mProfStackInit (inline)
0 0% 96.98% 298 42.82% runtime.main
0 0% 96.98% 6 0.86% runtime.makeProfStackFP (inline)
0 0% 96.98% 6 0.86% runtime.makeslice
0 0% 96.98% 20 2.87% runtime.malg
0 0% 96.98% 12 1.72% runtime.mcall
0 0% 96.98% 12 1.72% runtime.mcommoninit
0 0% 96.98% 3 0.43% runtime.mpreinit (inline)
0 0% 96.98% 3 0.43% runtime.newMWeakPointer (inline)
0 0% 96.98% 19 2.73% runtime.newm
0 0% 96.98% 41 5.89% runtime.newobject
0 0% 96.98% 16 2.30% runtime.newproc.func1
0 0% 96.98% 16 2.30% runtime.newproc1
0 0% 96.98% 12 1.72% runtime.park_m
0 0% 96.98% 7 1.01% runtime.resetspinning
0 0% 96.98% 13 1.87% runtime.schedule
0 0% 96.98% 6 0.86% runtime.startTheWorldWithSema
0 0% 96.98% 13 1.87% runtime.startm
0 0% 96.98% 22 3.16% runtime.systemstack
0 0% 96.98% 13 1.87% runtime.wakep
0 0% 96.98% 16 2.30% strings.(*Builder).WriteRune
0 0% 96.98% 13 1.87% strings.Split (inline)
0 0% 96.98% 12 1.72% sync.(*Once).Do (partial-inline)
0 0% 96.98% 12 1.72% sync.(*Once).doSlow
0 0% 96.98% 12 1.72% sync.(*Pool).Get
0 0% 96.98% 10 1.44% sync.(*Pool).pin
0 0% 96.98% 24 3.45% testing.(*B).run
0 0% 96.98% 349 50.14% testing.(*B).run1.func1
0 0% 96.98% 3 0.43% testing.(*M).after
0 0% 96.98% 3 0.43% testing.(*M).after.func1
0 0% 96.98% 3 0.43% testing.(*M).writeProfiles
0 0% 96.98% 5 0.72% testing.(*T).Run
0 0% 96.98% 278 39.94% testing.(*matcher).fullName
0 0% 96.98% 8 1.15% testing.runBenchmarks.func1
0 0% 96.98% 6 0.86% testing.runExamples
0 0% 96.98% 5 0.72% testing.runTests.func1
0 0% 96.98% 255 36.64% testing.simpleMatch.matches
0 0% 96.98% 153 21.98% testing.simpleMatch.verify
0 0% 96.98% 8 1.15% testing.tRunner
0 0% 96.98% 399 57.33% testing/internal/testdeps.TestDeps.MatchString