skip to content
Alvin Lucillo

Live heap memory benchmark

/ 5 min read

Unlike alloc_space, inuse_space tracks heap memory that is live and not yet collected by garbage collection. In this perspective, there is not much significant difference between preallocated and growing versions. inuse_space is generally used for checking memory leaks.

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             86302 ns/op         741.58 MB/s      285437 B/op         19 allocs/op
PASS
ok      codec01 0.024s

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             27129 ns/op        2359.10 MB/s       65537 B/op          1 allocs/op
PASS
ok      codec01 0.016s
go tool pprof -top -inuse_space \
  /tmp/codec01-message-batch-growing-memory.pprof
File: codec01.test
Build ID: f4a53b5a12db4d35158b288d82f85a5798e33258
Type: inuse_space
Time: 2026-11-01 20:40:44 PST
Showing nodes accounting for 115.37kB, 99.06% of 116.46kB total
Dropped 64 nodes (cum <= 0.58kB)
      flat  flat%   sum%        cum   cum%
   72.22kB 62.01% 62.01%    72.22kB 62.01%  codec01.BuildMessageBatchGrowing (inline)
   36.19kB 31.07% 93.08%    36.19kB 31.07%  runtime.mallocgc
    3.52kB  3.02% 96.10%     3.52kB  3.02%  sync.(*Pool).pinSlow
    1.80kB  1.54% 97.65%     1.80kB  1.54%  regexp.onePassCopy
    1.38kB  1.18% 98.83%     1.38kB  1.18%  regexp/syntax.(*compiler).inst (inline)
    0.18kB  0.15% 98.98%     3.59kB  3.08%  regexp.compile
    0.09kB  0.08% 99.06%     1.86kB  1.60%  regexp.newOnePassMachine (inline)
         0     0% 99.06%     7.52kB  6.46%  codec01.BenchmarkBuildMessageBatch
         0     0% 99.06%    72.33kB 62.11%  codec01.BenchmarkBuildMessageBatch.func1
         0     0% 99.06%     1.95kB  1.68%  fmt.Fprintf
         0     0% 99.06%     1.95kB  1.68%  fmt.newPrinter
         0     0% 99.06%     1.86kB  1.60%  regexp.(*Regexp).MatchString (inline)
         0     0% 99.06%     1.86kB  1.60%  regexp.(*Regexp).doExecute
         0     0% 99.06%     1.86kB  1.60%  regexp.(*Regexp).doMatch (inline)
         0     0% 99.06%     1.86kB  1.60%  regexp.(*Regexp).doOnePass
         0     0% 99.06%     3.59kB  3.08%  regexp.Compile (inline)
         0     0% 99.06%     1.81kB  1.56%  regexp.compileOnePass
         0     0% 99.06%     1.38kB  1.18%  regexp/syntax.(*compiler).compile
         0     0% 99.06%     1.38kB  1.18%  regexp/syntax.(*compiler).rune
         0     0% 99.06%     1.42kB  1.22%  regexp/syntax.Compile
         0     0% 99.06%    22.78kB 19.56%  runtime.allocm
         0     0% 99.06%     6.55kB  5.62%  runtime.gcBgMarkWorker
         0     0% 99.06%     5.20kB  4.46%  runtime.gcStart.func4
         0     0% 99.06%        9kB  7.73%  runtime.mProfStackInit (inline)
         0     0% 99.06%        9kB  7.73%  runtime.makeProfStackFP (inline)
         0     0% 99.06%        9kB  7.73%  runtime.makeslice
         0     0% 99.06%    10.31kB  8.85%  runtime.malg
         0     0% 99.06%    10.39kB  8.92%  runtime.mcall
         0     0% 99.06%    10.91kB  9.36%  runtime.mcommoninit
         0     0% 99.06%     1.88kB  1.61%  runtime.mpreinit (inline)
         0     0% 99.06%     7.20kB  6.18%  runtime.mstart
         0     0% 99.06%     7.20kB  6.18%  runtime.mstart0
         0     0% 99.06%     7.20kB  6.18%  runtime.mstart1
         0     0% 99.06%    22.78kB 19.56%  runtime.newm
         0     0% 99.06%    26.89kB 23.09%  runtime.newobject
         0     0% 99.06%     6.81kB  5.85%  runtime.newproc.func1
         0     0% 99.06%     6.81kB  5.85%  runtime.newproc1
         0     0% 99.06%    10.39kB  8.92%  runtime.park_m
         0     0% 99.06%    12.39kB 10.64%  runtime.resetspinning
         0     0% 99.06%    17.59kB 15.10%  runtime.schedule
         0     0% 99.06%     5.20kB  4.46%  runtime.startTheWorldWithSema
         0     0% 99.06%    17.59kB 15.10%  runtime.startm
         0     0% 99.06%    12.01kB 10.31%  runtime.systemstack
         0     0% 99.06%    17.59kB 15.10%  runtime.wakep
         0     0% 99.06%     2.05kB  1.76%  sync.(*Once).Do (inline)
         0     0% 99.06%     2.05kB  1.76%  sync.(*Once).doSlow
         0     0% 99.06%     3.72kB  3.19%  sync.(*Pool).Get
         0     0% 99.06%     3.52kB  3.02%  sync.(*Pool).pin
         0     0% 99.06%     7.52kB  6.46%  testing.(*B).Run
         0     0% 99.06%     2.08kB  1.78%  testing.(*B).run
         0     0% 99.06%     1.95kB  1.68%  testing.(*B).run.func1
         0     0% 99.06%    79.85kB 68.57%  testing.(*B).run1.func1
         0     0% 99.06%    79.85kB 68.57%  testing.(*B).runN
         0     0% 99.06%     5.45kB  4.68%  testing.(*matcher).fullName
         0     0% 99.06%     5.45kB  4.68%  testing.simpleMatch.matches
         0     0% 99.06%     5.45kB  4.68%  testing/internal/testdeps.TestDeps.MatchString

go tool pprof -top -inuse_space \
  /tmp/codec01-message-batch-preallocated-memory.pprof
File: codec01.test
Build ID: f4a53b5a12db4d35158b288d82f85a5798e33258
Type: inuse_space
Time: 2026-11-01 20:40:48 PST
Showing nodes accounting for 100kB, 98.79% of 101.23kB total
Dropped 59 nodes (cum <= 0.51kB)
      flat  flat%   sum%        cum   cum%
      64kB 63.22% 63.22%       64kB 63.22%  codec01.BuildMessageBatchPreallocated (inline)
   30.63kB 30.26% 93.49%    30.63kB 30.26%  runtime.mallocgc
    2.05kB  2.02% 95.51%     2.05kB  2.02%  regexp.onePassCopy
    1.76kB  1.74% 97.24%     1.76kB  1.74%  sync.(*Pool).pinSlow
    1.38kB  1.36% 98.60%     1.38kB  1.36%  regexp/syntax.(*compiler).inst (inline)
    0.19kB  0.19% 98.79%     3.84kB  3.80%  regexp.compile
         0     0% 98.79%     6.05kB  5.98%  codec01.BenchmarkBuildMessageBatch
         0     0% 98.79%       64kB 63.22%  codec01.BenchmarkBuildMessageBatch.func2
         0     0% 98.79%     2.10kB  2.08%  fmt.Fprintf
         0     0% 98.79%     1.96kB  1.94%  fmt.newPrinter
         0     0% 98.79%     0.54kB  0.53%  main.main
         0     0% 98.79%     3.84kB  3.80%  regexp.Compile (inline)
         0     0% 98.79%     2.06kB  2.04%  regexp.compileOnePass
         0     0% 98.79%     1.38kB  1.36%  regexp/syntax.(*compiler).compile
         0     0% 98.79%     1.38kB  1.36%  regexp/syntax.(*compiler).rune
         0     0% 98.79%     1.42kB  1.40%  regexp/syntax.Compile
         0     0% 98.79%    17.59kB 17.37%  runtime.allocm
         0     0% 98.79%     6.22kB  6.14%  runtime.gcBgMarkWorker
         0     0% 98.79%     5.20kB  5.13%  runtime.gcStart.func4
         0     0% 98.79%     6.75kB  6.67%  runtime.mProfStackInit (inline)
         0     0% 98.79%     0.54kB  0.53%  runtime.main
         0     0% 98.79%     6.75kB  6.67%  runtime.makeProfStackFP (inline)
         0     0% 98.79%     6.75kB  6.67%  runtime.makeslice
         0     0% 98.79%     9.38kB  9.26%  runtime.malg
         0     0% 98.79%     5.20kB  5.13%  runtime.mcall
         0     0% 98.79%     8.18kB  8.08%  runtime.mcommoninit
         0     0% 98.79%     1.41kB  1.39%  runtime.mpreinit (inline)
         0     0% 98.79%     7.20kB  7.11%  runtime.mstart
         0     0% 98.79%     7.20kB  7.11%  runtime.mstart0
         0     0% 98.79%     7.20kB  7.11%  runtime.mstart1
         0     0% 98.79%    17.59kB 17.37%  runtime.newm
         0     0% 98.79%    23.62kB 23.33%  runtime.newobject
         0     0% 98.79%     6.81kB  6.73%  runtime.newproc.func1
         0     0% 98.79%     6.81kB  6.73%  runtime.newproc1
         0     0% 98.79%     5.20kB  5.13%  runtime.park_m
         0     0% 98.79%     7.20kB  7.11%  runtime.resetspinning
         0     0% 98.79%    12.39kB 12.24%  runtime.schedule
         0     0% 98.79%     5.20kB  5.13%  runtime.startTheWorldWithSema
         0     0% 98.79%    12.39kB 12.24%  runtime.startm
         0     0% 98.79%    12.01kB 11.86%  runtime.systemstack
         0     0% 98.79%    12.39kB 12.24%  runtime.wakep
         0     0% 98.79%     2.06kB  2.04%  sync.(*Once).Do (inline)
         0     0% 98.79%     2.06kB  2.04%  sync.(*Once).doSlow
         0     0% 98.79%     1.96kB  1.94%  sync.(*Pool).Get
         0     0% 98.79%     1.76kB  1.74%  sync.(*Pool).pin
         0     0% 98.79%     6.16kB  6.09%  testing.(*B).Run
         0     0% 98.79%     2.10kB  2.08%  testing.(*B).run
         0     0% 98.79%     1.96kB  1.94%  testing.(*B).run.func1
         0     0% 98.79%    70.05kB 69.21%  testing.(*B).run1.func1
         0     0% 98.79%    70.16kB 69.31%  testing.(*B).runN
         0     0% 98.79%     0.54kB  0.53%  testing.(*M).Run
         0     0% 98.79%     3.84kB  3.80%  testing.(*matcher).fullName
         0     0% 98.79%     3.84kB  3.80%  testing.simpleMatch.matches
         0     0% 98.79%     3.84kB  3.80%  testing/internal/testdeps.TestDeps.MatchString