为了账号安全,请及时绑定邮箱和手机立即绑定

计算地图的内存占用(或字节长度)

计算地图的内存占用(或字节长度)

Go
杨魅力 2021-10-25 20:28:48
我想将地图限制为最大 X 字节。不过,似乎没有直接的方法来计算映射的字节长度。"encoding/binary"包有一个很好的Size功能,但它只适用于切片或“固定值”,不适用于地图。我可以尝试从地图中获取所有键/值对,推断它们的类型(如果它是 a map[string]interface{})并计算长度 - 但这既麻烦又可能不正确(因为这会排除地图的“内部”Go成本本身 - 管理指向元素的指针等)。有什么建议的方法吗?最好是代码示例。
查看完整描述

1 回答

?
江户川乱折腾

TA贡献1851条经验 获得超5个赞

这是地图标题的定义:


// 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的值和地图值类型的值。


您必须hmap通过程序集与包共享结构,类似于thunk.s运行时。


查看完整回答
反对 回复 2021-10-25
  • 1 回答
  • 0 关注
  • 249 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号