Search code examples
godata-structureshashtable

What's the memory cost/overhead of declaring one empty map[string]interface{} in golang?


Asking just for curiousity, from the source code

type hmap struct {
    count     int // 1 word
    flags     uint8
    B         uint8  
    noverflow uint16 
    hash0     uint32 // = 8 byte

    buckets    unsafe.Pointer // 1 word
    oldbuckets unsafe.Pointer // 1 word
    nevacuate  uintptr        // 1 word

    extra *mapextra // 1 word
}

so it would at least: 5 word + 8 byte

but why the creation cost is 0? -


package main

import (
    "fmt"
    "runtime"
)

func main() {
    var m1, m2 runtime.MemStats
    var i byte

    runtime.ReadMemStats(&m1)
    mp := map[byte]byte{}
    runtime.ReadMemStats(&m2)
    fmt.Println("Bytes allocated on creation:", m2.Alloc-m1.Alloc)
    for i = 0; i < 100; i++ {
        runtime.ReadMemStats(&m1)
        mp[i] = i
        runtime.ReadMemStats(&m2)
        fmt.Printf("Bytes allocated on assignment %d: %d\n", i, m2.Alloc-m1.Alloc)
    }
}

Playground: https://play.golang.org/p/iyYshDzexQt

Output:

Bytes allocated on creation: 0

Solution

  • why the creation cost is 0?

    A Go stack allocation allocates zero bytes on the heap.

    mp := map[byte]byte{}
    
    main map[byte]byte literal does not escape
    

    package main
    
    import (
        "fmt"
        "runtime"
    )
    
    func main() {
        var m1, m2 runtime.MemStats
        var i byte
    
        runtime.ReadMemStats(&m1)
        mp := map[byte]byte{}
        runtime.ReadMemStats(&m2)
        fmt.Println("Bytes allocated on creation:", m2.Alloc-m1.Alloc)
        for i = 0; i < 100; i++ {
            runtime.ReadMemStats(&m1)
            mp[i] = i
            runtime.ReadMemStats(&m2)
            fmt.Printf("Bytes allocated on assignment %d: %d\n", i, m2.Alloc-m1.Alloc)
        }
    }
    

    Playground: https://play.golang.org/p/iyYshDzexQt

    Output:

    Bytes allocated on creation: 0