dongrou5254 2019-09-25 06:59
浏览 104

如何计算复杂映射变量的内存大小

I am trying to calculate the size of a variable (EntryMap) in memory.

type Entry interface {
    A() string
    B() string
    C() time.Time
    D() int64
}

type Entries []Entry
type EntryMap map[string]Entries

What I got so far is this:

hm = make(EntryMap)
SizeInMem = 8 //Length of Empty HMap

for key, value := range hm {
    A = 8 // For each Key Value Assignment
    B = len(Key) + 1 // Size of key string var
    C = len(Entries) * unsafe.Sizeof(Entry) // Size of Entry object * len of slice
    SizeInMem += A+B+C
}

fmt.Println("Size in Memory &v", SizeInMem)

Is this the correct way to determine the size of variable in memory?

  • 写回答

1条回答 默认 最新

  • doutang2017 2019-09-25 17:13
    关注

    This is not a thing that you can calculate just in time...

    But I prepared test playground for you.

    https://play.golang.org/p/V_2fheWHQn-

    
    type Entry interface {
        A() string
        B() string
        D() int64
    }
    
    type Entries []Entry
    type EntryMap map[string]Entries
    
    func main() {
    
        size := memcalc(func() {
            /* Put your code here */
            var hm = make(EntryMap)
    
            // do something with map, grow, for example        
    
            // pass variable to this function to measure
            calc(hm)
        })
    
        fmt.Println(size)
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波