什么';在golang中声明一个空map[string]接口{}的内存成本/开销是多少?

什么';在golang中声明一个空map[string]接口{}的内存成本/开销是多少?,go,data-structures,hashtable,Go,Data Structures,Hashtable,只是为了好奇,从 所以它至少是:5个字+8个字节 但是为什么会这样呢- 为什么会这样 Go堆栈分配在堆上分配零字节 mp := map[byte]byte{} main map[byte]byte literal does not escape 很好,我也找到了第一个问题的答案:(每个地图大约170字节) package main import ( "fmt" "runtime" ) func main() { var m1, m2 runtime.MemSt

只是为了好奇,从

所以它至少是:5个字+8个字节

但是为什么会这样呢-


为什么会这样

Go堆栈分配在堆上分配零字节

mp := map[byte]byte{}

main map[byte]byte literal does not escape


很好,我也找到了第一个问题的答案:(每个地图大约170字节)
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)
    }
}
Bytes allocated on creation: 0
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)
    }
}
Bytes allocated on creation: 0