go slice array allocation gotcha

February 21, 2017    general blog

Why is memory is allocated differently when you preallocate a slice and an array in golang.

slicey := make([]byte, 1024*1024)

top shows there is no extra memory allocated while a program with that slice is running.

arr := [1024*1024]byte{}

top shows that memory is allocated while that program is running.

I am baffled why there is difference when slice also has a backing array that need to be initialized?

Program you can run

package main

import (

func main() {
	time.Sleep(10 * time.Second)
	slicey := make([]byte, 1024*1024)

go run and check top

Note - I ran this on Mac OSX El Capitan