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