My benchmark runs out of memory

I just happen to allocate a slice of int, of size b.N in my benchmark function.

It is all good for when b.N is:

However, when b.N is higher I get:
runtime stack:
runtime.throw(0x6dac98, 0x16)
/usr/local/go/src/runtime/panic.go:616 +0x81
runtime.sysMap(0xc4504b0000, 0x3b9ad0000, 0x0, 0x876f98)
/usr/local/go/src/runtime/mem_linux.go:216 +0x20a
runtime.(*mheap).sysAlloc(0x85c7a0, 0x3b9ad0000, 0x0)
/usr/local/go/src/runtime/malloc.go:470 +0xd4
runtime.(*mheap).grow(0x85c7a0, 0x1dcd65, 0x0)
/usr/local/go/src/runtime/mheap.go:907 +0x60
runtime.(*mheap).allocSpanLocked(0x85c7a0, 0x1dcd65, 0x876fa8, 0x0)
/usr/local/go/src/runtime/mheap.go:820 +0x301
runtime.(*mheap).alloc_m(0x85c7a0, 0x1dcd65, 0xffffffffffff0101, 0x7f959b7fddb8)
/usr/local/go/src/runtime/mheap.go:686 +0x118
/usr/local/go/src/runtime/mheap.go:753 +0x4d
runtime.(*mheap).alloc(0x85c7a0, 0x1dcd65, 0x10101, 0x6e9a28)
/usr/local/go/src/runtime/mheap.go:752 +0x8a
runtime.largeAlloc(0x3b9aca000, 0x450101, 0x456000)
/usr/local/go/src/runtime/malloc.go:826 +0x94
/usr/local/go/src/runtime/malloc.go:721 +0x46
/usr/local/go/src/runtime/asm_amd64.s:409 +0x79

What to do ? Can I tell it to not make b.N that high ?

The code benchmarked is a method that searches into an array. I randomly generate this array of size b.N. Should I change this strategy, how to I benchmark this search function ?

But isn’t N really how many times the test should run? has this

Functions of the form

func BenchmarkXxx(*testing.B)
are considered benchmarks, and are executed by the “go test” command when its -bench flag is provided. Benchmarks are run sequentially.

For a description of the testing flags, see

A sample benchmark function looks like this:

func BenchmarkHello(b *testing.B) {
    for i := 0; i < b.N; i++ {

The benchmark function must run the target code b.N times. During benchmark execution, b.N is adjusted until the benchmark function lasts long enough to be timed reliably

and not how much memory to allocate. You could of course allocate different slices with different sizes in the benchmark but not by using N

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.