小编典典

计算映射的内存占用量(或字节长度)

go

我想将地图限制为最多X个字节。似乎没有直接的方法可以计算映射的字节长度。

"encoding/binary"package具有不错的Size功能,但仅适用于切片或“固定值”,不适用于地图。

我可以尝试从地图中获取所有键/值对,推断它们的类型(如果为map[string]interface{})并计算长度-
但这既麻烦又可能不正确(因为这会排除地图的“内部”运行成本)本身-管理指向元素的指针等)。

有建议的方式吗?最好是一个代码示例。


阅读 294

收藏
2020-07-02

共1个答案

小编典典

这是地图标题的定义

// A header for a Go map.
type hmap struct {
    // Note: the format of the Hmap is encoded in ../../cmd/gc/reflect.c and
    // ../reflect/type.go.  Don't change this structure without also changing that code!
    count int // # live cells == size of map.  Must be first (used by len() builtin)
    flags uint32
    hash0 uint32 // hash seed
    B     uint8  // log_2 of # of buckets (can hold up to loadFactor * 2^B items)

    buckets    unsafe.Pointer // array of 2^B Buckets. may be nil if count==0.
    oldbuckets unsafe.Pointer // previous bucket array of half the size, non-nil only when growing
    nevacuate  uintptr        // progress counter for evacuation (buckets less than this have been evacuated)
}

计算其大小非常简单(unsafe.Sizeof)。

这是地图指向的每个单独存储区的定义:

// A bucket for a Go map.
type bmap struct {
    tophash [bucketCnt]uint8
    // Followed by bucketCnt keys and then bucketCnt values.
    // NOTE: packing all the keys together and then all the values together makes the
    // code a bit more complicated than alternating key/value/key/value/... but it allows
    // us to eliminate padding which would be needed for, e.g., map[int64]int8.
    // Followed by an overflow pointer.
}

bucketCnt 是一个常量,定义为:

bucketCnt     = 1 << bucketCntBits // equals decimal 8
bucketCntBits = 3

最终计算将为:

unsafe.Sizeof(hmap) + (len(theMap) * 8) + (len(theMap) * 8 * unsafe.Sizeof(x)) + (len(theMap) * 8 * unsafe.Sizeof(y))

theMap地图值在哪里,x是地图键类型y的值和地图值类型的值。

hmapthunk.s运行时类似,您必须通过组装与包共享结构。

2020-07-02