Test memory consumption
go
Solution
Actually it's very simple:
- Read memstats with `runtime.ReadMemStats(&m)` into `m`
- Invoke `f()`
- Read memstats again into `m2`
- Calculate diff between `m` and `m2`
For example:
var m1, m2 runtime.MemStats
runtime.GC()
runtime.ReadMemStats(&m1)
f()
runtime.ReadMemStats(&m2)
fmt.Println("total:", m2.TotalAlloc - m1.TotalAlloc)
fmt.Println("mallocs:", m2.Mallocs - m1.Mallocs)
Problem
I need to verify how much memory a specific function consumes during execution, and make sure that it stays under a specific limit. Ideally I'd like to do this in a test or benchmark. As far as I can see the only way to do this is to create a separate test binary and use the `BenchmarkResult` from ``` func Benchmark(f func(b *B)) BenchmarkResult ``` Is this the correct way to do this?