create a test-file
touch main_test.go
paste the code for a test:
package main
import (
"fmt"
"sync"
"testing"
"time"
)
func TestRoutines(t *testing.T) {
numRoutines := 1000000
var wg sync.WaitGroup
for i := 0; i < numRoutines; i++ {
wg.Add(1)
go func() {
defer wg.Done()
time.Sleep(10 * time.Second)
}()
}
wg.Wait()
fmt.Println("All goroutines finished.")
}
run the test with profiling:
go test -bench=. -benchmem -memprofile memprofile.out
inspect the results with pprof:
go tool pprof memprofile.out
use the command top
to see the top memory hogs. The output on my machine is this:
File: shipmap.test
Type: alloc_space
Time: May 26, 2023 at 8:19am (CEST)
Entering interactive mode (type "help" for commands, "o" for options)
(pprof) top
Showing nodes accounting for 561.63MB, 99.27% of 565.78MB total
Dropped 34 nodes (cum <= 2.83MB)
Showing top 10 nodes out of 21
flat flat% sum% cum cum%
406.16MB 71.79% 71.79% 406.16MB 71.79% runtime.malg
67.51MB 11.93% 83.72% 67.51MB 11.93% time.Sleep
36.60MB 6.47% 90.19% 36.60MB 6.47% runtime.allgadd
33.36MB 5.90% 96.08% 33.36MB 5.90% runtime.doaddtimer
14.50MB 2.56% 98.65% 14.50MB 2.56% shipmap.TestRoutines
3.50MB 0.62% 99.27% 3.50MB 0.62% runtime.allocm
0 0% 99.27% 34.36MB 6.07% runtime.mcall
0 0% 99.27% 33.36MB 5.90% runtime.modtimer
0 0% 99.27% 3.50MB 0.62% runtime.newm
0 0% 99.27% 442.77MB 78.26% runtime.newproc.func1