dpdyh86002 2015-08-06 05:34
浏览 102
已采纳

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

I want to limit a map to be maximum X bytes. It seems there is no straightforward way of computing the byte length of a map though.

"encoding/binary" package has a nice Size function, but it only works for slices or "fixed values", not for maps.

I could try to get all key/value pairs from the map, infer their type (if it's a map[string]interface{}) and compute the length - but that would be both cumbersome and probably incorrect (because that would exclude the "internal" Go cost of the map itself - managing pointers to elements etc).

Any suggested way of doing this? Preferably a code example.

  • 写回答

1条回答 默认 最新

  • douwang4374 2015-08-06 11:55
    关注

    This is the definition for a map header:

    // 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)
    }
    

    Calculating its size is pretty straightforward (unsafe.Sizeof).

    This is the definition for each individual bucket the map points to:

    // 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 is a constant defined as:

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

    The final calculation would be:

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

    Where theMap is your map value, x is a value of the map's key type and y a value of the map's value type.

    You'll have to share the hmap structure with your package via assembly, analogously to thunk.s in the runtime.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?